Merge pull request #937 from luoyunchong/dev

支持Fluent API .以继承接口的形式配置实体的。
This commit is contained in:
2881099
2022-05-24 17:54:20 +08:00
committed by GitHub
5 changed files with 144 additions and 2 deletions

View File

@ -1,3 +1,4 @@
using efcore_to_freesql.Entitys;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
@ -14,6 +15,15 @@ namespace efcore_to_freesql.DBContexts
{
base.OnModelCreating(modelBuilder);
Fsql.CodeFirst.ConfigEntity(modelBuilder.Model); //ͬ<><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>
Fsql.CodeFirst.ApplyConfiguration(new SongConfiguration());
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Fsql.CodeFirst.ApplyConfigurationsFromAssembly(typeof(SongConfiguration).Assembly);
Fsql.CodeFirst.SyncStructure<Song>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

View File

@ -1,5 +1,6 @@
using efcore_to_freesql.Entitys;
using FreeSql;
using FreeSql.Extensions.EfCoreFluentApi;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
@ -178,4 +179,27 @@ public static class CodeFirstExtensions
cf.SyncStructure<SongType>();
cf.SyncStructure<Song>();
}
}
}
public class SongConfiguration : FreeSql.Extensions.EfCoreFluentApi.IEntityTypeConfiguration<Song>
{
public void Configure(EfCoreTableFluent<Song> eb)
{
eb.ToTable("tb_song_config");
eb.Ignore(a => a.Field1);
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
eb.Property(a => a.Url).HasMaxLength(100);
eb.Property(a => a.RowVersion).IsRowVersion();
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
eb.HasKey(a => a.Id);
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_tb_song1111");
//一对多、多对一
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
//多对多
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
}
}

View File

@ -28,7 +28,7 @@ namespace efcore_to_freesql
.Build();
//Fsql.CodeFirst.EfCoreFluentApiTestGeneric();
Fsql.CodeFirst.EfCoreFluentApiTestDynamic();
//Fsql.CodeFirst.EfCoreFluentApiTestDynamic();
BaseDBContext.Fsql = Fsql;