public class Model
{
[Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Column("create_time")]
public DateTime CreTime { get; set; } = DateTime.Now;
[Column("create_uid")]
public long? CreUid { get; set; }
[Column("update_time")]
public DateTime? UpdTime { get; set; }
[Column("update_uid")]
public long? UpdUid { get; set; }
[Column("remark")]
public string Remark { get; set; }
[Column("is_del")]
public bool IsDel { get; set; } = false;
} [Table("a_user_type")]
public class UserCls
{
[Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public long Id { get; set; }
[Column("des")]
public string Des { get; set; }
} [Table("b_user")]
public class User : Model
{
[Column("account"), Index(IsUnique = false)]
public string Account { get; set; }
[Column("passwd")]
public string Passwd { get; set; }
[Column("label")]
public string Label { get; set; }
[Column("cls_id"), ForeignKey("Cls")]
public long? ClsId { get; set; }
public virtual UserCls Cls { get; set; }
}
以上数据库字段全部取小写,是因为postgres非常严格,驼峰式写法的sql要加双引号,非常麻烦。
mssqlserver 可以完全不用管,mysql只需注意表的名字小写即可,postgres连字段名都要特别注意。
public class MesDbContext: DbContext
{
public MesDbContext():base("name=mes")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MesDbContext, MigrateConfig>());
} public DbSet<User> Users { get; set; }
public DbSet<UserCls> UserCls { get; set; }
} public class MigrateConfig : DbMigrationsConfiguration<MesDbContext>
{
public MigrateConfig()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<!-- 注意这里,安装程序包时,这里的配置并不会自动添加 -->
<remove invariant="Npgsql" />
<add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net45" />
<package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net45" />
<package id="Npgsql" version="3.2.5" targetFramework="net45" />
<package id="System.Threading.Tasks.Extensions" version="4.4.0" targetFramework="net45" />
</packages>