gpt4 book ai didi

asp.net-core - 注册实体映射

转载 作者:行者123 更新时间:2023-12-01 00:40:34 24 4
gpt4 key购买 nike

我在 工作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.
}}

错误
enter image description here

那么 nopCommerce 使用 DbModelBuilder 来自命名空间: System.Data.Entity 我使用 模型构建器 命名空间下: Microsoft.EntityFrameworkCore .

因此,如果您有任何解决方案或建议。请让我知道。

最佳答案

在 EF Core 中,没有 Configurations可在 ModeBuilder 获得房产as of now .

解决此问题的一种方法是将您的配置更改为

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

然后您可以将它们添加到您的 DbContext 中 OnModelCreating像这样的方法:
foreach (var type in typesToRegister)
{
Activator.CreateInstance(type, modelBuilder);
}

关于asp.net-core - 注册实体映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com