4

私はEF coreを使用して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>();
    }

国.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は名前空間System.Data.EntityからDbModelBuilderを使用し、名前空間Microsoft.EntityFrameworkCoreの下でModelBuilderを使用します。

それで、解決策や推奨事項があれば。私にお知らせください。

4

1 に答える 1