Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持Fluent API .以继承接口的形式配置实体的。 #937

Merged
merged 2 commits into from
May 24, 2022
Merged

支持Fluent API .以继承接口的形式配置实体的。 #937

merged 2 commits into from
May 24, 2022

Conversation

luoyunchong
Copy link
Collaborator

@luoyunchong luoyunchong commented Nov 11, 2021

Feature 特性

支持Fluent API .以继承接口的形式配置实体的。
旧行为

fsql.CodeFirst.Entity<Song>(eb => {
    eb.ToTable("tb_song");
    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 => new { a.Id, a.Title }).IsUnique().HasName("idx_xxx11");

    //一对多、多对一
    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));
});

新行为,我们可以将实体类和配置一一对应。

public class SongConfiguration : FreeSql.Extensions.EfCoreFluentApi.IEntityTypeConfiguration<Song>
{
    public void Configure(EfCoreTableFluent<Song> eb)
    {
        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);

        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));
    }
}

     

二种使用方式
1.单个配置

codefirst.ApplyConfiguration(new SongConfiguration());

2.批量配置

codefirst.ApplyConfigurationsFromAssembly(typeof(SongConfiguration).Assembly);

简要描述原因

分离结构

使用场景

配置FluentAPI.

.net framework4.0不支持

@luoyunchong luoyunchong reopened this Apr 27, 2022
@luoyunchong luoyunchong requested a review from 2881099 May 24, 2022 09:51
@2881099 2881099 merged commit a1f029a into dotnetcore:master May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants