postgres entityframework foreignkey
1 public class Model 2 { 3 [Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 4 public long Id { get; set; } 5 [Column("create_time")] 6 public DateTime CreTime { get; set; } = DateTime.Now; 7 [Column("create_uid")] 8 public long? CreUid { get; set; } 9 [Column("update_time")] 10 public DateTime? UpdTime { get; set; } 11 [Column("update_uid")] 12 public long? UpdUid { get; set; } 13 [Column("remark")] 14 public string Remark { get; set; } 15 [Column("is_del")] 16 public bool IsDel { get; set; } = false; 17 } 18 19 [Table("a_user_type")] 20 public class UserCls 21 { 22 [Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.None)] 23 public long Id { get; set; } 24 [Column("des")] 25 public string Des { get; set; } 26 } 27 28 [Table("b_user")] 29 public class User : Model 30 { 31 [Column("account"), Index(IsUnique = false)] 32 public string Account { get; set; } 33 [Column("passwd")] 34 public string Passwd { get; set; } 35 [Column("label")] 36 public string Label { get; set; } 37 [Column("cls_id"), ForeignKey("Cls")] 38 public long? ClsId { get; set; } 39 public virtual UserCls Cls { get; set; } 40 }
以上数据库字段全部取小写,是因为postgres非常严格,驼峰式写法的sql要加双引号,非常麻烦。
mssqlserver 可以完全不用管,mysql只需注意表的名字小写即可,postgres连字段名都要特别注意。
1 public class MesDbContext: DbContext 2 { 3 public MesDbContext():base("name=mes") 4 { 5 Database.SetInitializer(new MigrateDatabaseToLatestVersion<MesDbContext, MigrateConfig>()); 6 } 7 8 public DbSet<User> Users { get; set; } 9 public DbSet<UserCls> UserCls { get; set; } 10 } 11 12 public class MigrateConfig : DbMigrationsConfiguration<MesDbContext> 13 { 14 public MigrateConfig() 15 { 16 AutomaticMigrationsEnabled = true; 17 AutomaticMigrationDataLossAllowed = true; 18 } 19 }
1 <entityFramework> 2 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 3 <providers> 4 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 5 <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /> 6 </providers> 7 </entityFramework> 8
1 <system.data> 2 <DbProviderFactories> 3 <!-- 注意这里,安装程序包时,这里的配置并不会自动添加 --> 4 <remove invariant="Npgsql" /> 5 <add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" /> 6 </DbProviderFactories> 7 </system.data> 8
1 <?xml version="1.0" encoding="utf-8"?> 2 <packages> 3 <package id="EntityFramework" version="6.2.0" targetFramework="net45" /> 4 <package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net45" /> 5 <package id="Npgsql" version="3.2.5" targetFramework="net45" /> 6 <package id="System.Threading.Tasks.Extensions" version="4.4.0" targetFramework="net45" /> 7 </packages>
posted on 2018-02-10 09:47 jonney_wang 阅读(303) 评论(0) 编辑 收藏 举报