我正在使用EF core在asp.net core项目中工作。我通过覆盖上下文类中的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; }
}
国家地图.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 使用命名空间中的DbModelBuilder:System.Data.Entity,我使用命名空间下的ModelBuilder:Microsoft.EntityFrameworkCore。
因此,如果您有任何解决方案或建议。请告诉我。
