第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

實現(xiàn)接口的實體的通用配置

實現(xiàn)接口的實體的通用配置

C#
阿晨1998 2021-11-21 15:13:03
假設我有一些界面,例如:public interface ISoftDeletable{    bool IsActive { get; set }}我有很多實體實現(xiàn)了它:public class Entity1 : ISoftDeletable{    public int Id { get; set }    public bool IsActive { get; set; }}public class Entity2 : ISoftDeletable{    public int Id { get; set }    public bool IsActive { get; set; }}在OnModelCreating:protected override void OnModelCreating(ModelBuilder modelBuilder){    modelBuilder.Entity<Entity1>().Property(e => e.IsActive).HasDefaultValue(true);    modelBuilder.Entity<Entity2>().Property(e => e.IsActive).HasDefaultValue(true);}有什么方法可以重構它,以便我可以HasDefaultValue為所有實體進行設置,ISoftDeletable而不是像上面那樣做?我可能可以使用每個實體的默認構造函數(shù)解決這個特定情況,IsActive = true甚至創(chuàng)建一個基本抽象類,但我不太喜歡它。有沒有更好的辦法?
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經(jīng)驗 獲得超4個贊

我在這里找到了一些答案:GetEntityTypes: configure entity properties using the generic version of .Property<TEntity> in EF Core


除了上面的評論之外,還有一種方法可以不用為每個實體調(diào)用它。這可能可以重構為一些擴展方法,正如 Erndob 在我的問題下的評論所提到的。


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())

    {

        if (typeof(ISoftDeletable).IsAssignableFrom(entityType.ClrType))

        {

            modelBuilder.Entity(entityType.ClrType).Property<bool>(nameof(ISoftDeletable.IsActive)).HasDefaultValue(true);

        }

    }

}

解決方案是使用ModelBuilder.Model.GetEntityTypes()和查找可從ISoftDeletable.


在我看來,這比手動配置它甚至創(chuàng)建一個抽象IEntityTypeConfiguration<>類要好得多,因為您不必記住對所有ISoftDeletable類都使用它。


更干凈的外觀:


public static class ModelBuilderExtensions

{

    public static ModelBuilder EntitiesOfType<T>(this ModelBuilder modelBuilder,

        Action<EntityTypeBuilder> buildAction) where T : class

    {

        return modelBuilder.EntitiesOfType(typeof(T), buildAction);

    }


    public static ModelBuilder EntitiesOfType(this ModelBuilder modelBuilder, Type type,

        Action<EntityTypeBuilder> buildAction)

    {

        foreach (var entityType in modelBuilder.Model.GetEntityTypes())

            if (type.IsAssignableFrom(entityType.ClrType))

                buildAction(modelBuilder.Entity(entityType.ClrType));


        return modelBuilder;

    }

}

并且OnModelCreating:


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    modelBuilder.EntitiesOfType<ISoftDeletable>(builder =>

    {

        builder.Property<bool>(nameof(ISoftDeletable.IsActive)).HasDefaultValue(true);


        // query filters :)

        var param = Expression.Parameter(builder.Metadata.ClrType, "p");

        var body = Expression.Equal(Expression.Property(param, nameof(ISoftDeletable.IsActive)), Expression.Constant(true));

        builder.HasQueryFilter(Expression.Lambda(body, param));

    });

}


查看完整回答
反對 回復 2021-11-21
?
白板的微信

TA貢獻1883條經(jīng)驗 獲得超3個贊

我想做類似的事情,但使用IEntityTypeConfiguration界面來保存我的通用配置。我最終不得不使用反射,但它有效:


Interface:


public interface IHasDisplayId

{

    Guid DisplayId { get; }

}

EntityTypeConfig:


public class HasDisplayIdEntityTypeConfiguration<T> : IEntityTypeConfiguration<T> where T : class, IHasDisplayId

{

    public void Configure(EntityTypeBuilder<T> builder)

    {

        builder.Property(e => e.DisplayId).IsRequired();

        builder.HasIndex(e => e.DisplayId);

    }

}

Extension method:


public static ModelBuilder ApplyConfiguration<T>(this ModelBuilder modelBuilder, Type configurationType, Type entityType)

{

    if (typeof(T).IsAssignableFrom(entityType))

    {

        // Build IEntityTypeConfiguration type with generic type parameter

        var configurationGenericType = configurationType.MakeGenericType(entityType);

        // Create an instance of the IEntityTypeConfiguration implementation

        var configuration = Activator.CreateInstance(configurationGenericType);

        // Get the ApplyConfiguration method of ModelBuilder via reflection

        var applyEntityConfigurationMethod = typeof(ModelBuilder)

            .GetMethods()

            .Single(e => e.Name == nameof(ModelBuilder.ApplyConfiguration)

                         && e.ContainsGenericParameters

                         && e.GetParameters().SingleOrDefault()?.ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>));

        // Create a generic ApplyConfiguration method with our entity type

        var target = applyEntityConfigurationMethod.MakeGenericMethod(entityType);

        // Invoke ApplyConfiguration, passing our IEntityTypeConfiguration instance

        target.Invoke(modelBuilder, new[] { configuration });

    }


    return modelBuilder;

}

Usage:


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())

    {

        modelBuilder.ApplyConfiguration<IHasDisplayId>(typeof(HasDisplayIdEntityTypeConfiguration<>), entityType.ClrType);

    }

}


查看完整回答
反對 回復 2021-11-21
  • 2 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號