我正在使用EF核心asp.net核心項目中工作。我通過覆蓋上下文類中的OnModelCreating函數來映射我的實體。我可以輕鬆地手動映射這些實體。好吧,我最好發布我的代碼並解釋..

這就是我在Context類中所做的:

protected override void OnModelCreating(ModelBuilder builder)
    {        
        base.OnModelCreating(builder);
        //builder.RegisterEntityMapping<CodeTable, CodeTableMap>();
        builder.RegisterEntityMapping<Country, CountryMap>();
        builder.RegisterEntityMapping<State, StateMap>();
        builder.RegisterEntityMapping<City, CityMap>();
        builder.RegisterEntityMapping<User, UserMap>();
        builder.RegisterEntityMapping<Prospect, ProspectMap>();
    }

Country.cs

public class Country:BaseEntity
{

    public string CountryCode { get; set; }
    public string CountryName { get; set; }
    public string Currency { get; set; }
    public string CurrencyName { get; set; }
    public string UnitOfMeasure { get; set; }
    public string TelephoneCountryCode { get; set; }
    public string CurrencySymbol { get; set; }

    public virtual ICollection<State> States { get; set; }
    public virtual ICollection<City> Cities { get; set; }
}

CountryMap.cs

public class CountryMap : QuantumEntityTypeConfiguration<Core.Domain.Country>
{
    public override void Map(EntityTypeBuilder<Core.Domain.Country> builder)
    {
        builder.ToTable("Country");
        builder.HasKey(pr => pr.CountryCode);

        builder.HasMany(m => m.Cities).WithOne(i=> i.Country).HasForeignKey(m=> m.CountryCode);
        builder.HasMany(m => m.States).WithOne(i => i.Country).HasForeignKey(m => m.CountryCode);
    }
}

但是,我想動態地進行動態操作,因為稍後會對所有模型進行映射。我可以在nopCommerce的上下文類中找到解決方案,他們以這種方式做到了:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //dynamically load all configuration
        //System.Type configType = typeof(LanguageMap);   //any of your configuration classes here
        //var typesToRegister = Assembly.GetAssembly(configType).GetTypes()

        var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type => !String.IsNullOrEmpty(type.Namespace))
        .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
            type.BaseType.GetGenericTypeDefinition() == typeof(NopEntityTypeConfiguration<>));
        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.Configurations.Add(configurationInstance);
        }
        //...or do it manually below. For example,
        //modelBuilder.Configurations.Add(new LanguageMap());



        base.OnModelCreating(modelBuilder);
    }

我試圖實現它,我收到一個錯誤:

 protected override void OnModelCreating(ModelBuilder builder)
    {
        // TODO: Use Dynamic mapping by getting classes which uses QuantumEntityTypeConfiguration
        var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type => !String.IsNullOrEmpty(type.Namespace))
        .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
            type.BaseType.GetGenericTypeDefinition() == typeof(QuantumEntityTypeConfiguration<>));
        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);                   
            builder.Configurations.Add(configurationInstance); //Error in this line specifying:ModelBuilder has no defination for Configuration.
        }}

錯誤 在此處輸入圖像描述

好吧nopCommerce使用命名空間中的DbModelBuilderSystem.Data.Entity ,我在命名空間下使用ModelBuilderMicrosoft.EntityFrameworkCore

所以,如果您有任何解決方案或建議。請告訴我。

一般承認的答案

在EF Core中, 截至目前ModeBuilder上沒有可用的Configurations屬性。

解決此問題的一種方法是將配置更改為

public class CountryMap : QuantumEntityTypeConfiguration<Country>
{
    public CountryMap(ModelBuilder modelBuilder)
    {
        EntityTypeBuilder<Country> builder = modelBuilder.Entity<Country>()

        builder.ToTable("Country");
        builder.HasKey(pr => pr.CountryCode);

        builder.HasMany(m => m.Cities).WithOne(i=> i.Country).HasForeignKey(m=> m.CountryCode);
        builder.HasMany(m => m.States).WithOne(i => i.Country).HasForeignKey(m => m.CountryCode);
    }
}

然後你可以在OnModelCreating方法的DbContext中添加它們,如下所示:

foreach (var type in typesToRegister)
{       
    Activator.CreateInstance(type, modelBuilder);
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow