- 增加 DbContext 与 EFCore 相似的 FluentApi 对动态类型的处理;#281

This commit is contained in:
28810
2020-04-20 21:13:44 +08:00
parent 67222ba007
commit b1660e87a5
7 changed files with 701 additions and 192 deletions

View File

@ -9,7 +9,7 @@ using FreeSql.Internal.CommonProvider;
partial class FreeSqlDbContextExtensions
{
/// <summary>
/// EFCore 99% 相似的 FluentApi 扩展方法
/// EFCore 95% 相似的 FluentApi 扩展方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="codeFirst"></param>
@ -21,13 +21,26 @@ partial class FreeSqlDbContextExtensions
codeFirst.ConfigEntity<T>(tf => modelBuilder(new EfCoreTableFluent<T>(cf._orm, tf)));
return codeFirst;
}
/// <summary>
/// EFCore 95% 相似的 FluentApi 扩展方法
/// </summary>
/// <param name="codeFirst"></param>
/// <param name="entityType">实体类型</param>
/// <param name="modelBuilder"></param>
/// <returns></returns>
public static ICodeFirst Entity(this ICodeFirst codeFirst, Type entityType, Action<EfCoreTableFluent> modelBuilder)
{
var cf = codeFirst as CodeFirstProvider;
codeFirst.ConfigEntity(entityType, tf => modelBuilder(new EfCoreTableFluent(cf._orm, tf, entityType)));
return codeFirst;
}
public static void EfCoreFluentApiTest(IFreeSql fsql)
public static void EfCoreFluentApiTestGeneric(IFreeSql fsql)
{
var cf = fsql.CodeFirst;
cf.Entity<Song>(eb =>
{
eb.ToTable("tb_song");
eb.ToTable("tb_song1");
eb.Ignore(a => a.Field1);
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
eb.Property(a => a.Url).HasMaxLength(100);
@ -36,7 +49,7 @@ partial class FreeSqlDbContextExtensions
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
eb.HasKey(a => a.Id);
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_xxx11");
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_tb_song1111");
//一对多、多对一
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
@ -46,6 +59,7 @@ partial class FreeSqlDbContextExtensions
});
cf.Entity<SongType>(eb =>
{
eb.ToTable("tb_songtype1");
eb.HasMany(a => a.Songs).WithOne(a => a.Type).HasForeignKey(a => a.TypeId);
eb.HasData(new[]
@ -76,6 +90,61 @@ partial class FreeSqlDbContextExtensions
cf.SyncStructure<Song>();
}
public static void EfCoreFluentApiTestDynamic(IFreeSql fsql)
{
var cf = fsql.CodeFirst;
cf.Entity(typeof(Song), eb =>
{
eb.ToTable("tb_song2");
eb.Ignore("Field1");
eb.Property("Title").HasColumnType("varchar(50)").IsRequired();
eb.Property("Url").HasMaxLength(100);
eb.Property("RowVersion").IsRowVersion();
eb.Property("CreateTime").HasDefaultValueSql("current_timestamp");
eb.HasKey("Id");
eb.HasIndex("Title").IsUnique().HasName("idx_tb_song2222");
//一对多、多对一
eb.HasOne("Type").HasForeignKey("TypeId").WithMany("Songs");
//多对多
eb.HasMany("Tags").WithMany("Songs", typeof(Song_tag));
});
cf.Entity(typeof(SongType), eb =>
{
eb.ToTable("tb_songtype2");
eb.HasMany("Songs").WithOne("Type").HasForeignKey("TypeId");
eb.HasData(new[]
{
new SongType
{
Id = 1,
Name = "流行",
Songs = new List<Song>(new[]
{
new Song{ Title = "真的爱你" },
new Song{ Title = "爱你一万年" },
})
},
new SongType
{
Id = 2,
Name = "乡村",
Songs = new List<Song>(new[]
{
new Song{ Title = "乡里乡亲" },
})
},
});
});
cf.SyncStructure<SongType>();
cf.SyncStructure<Song>();
}
public class SongType
{
public int Id { get; set; }