保留关系的 ModelBuilder 种子数据
ModelBuilder seed data with relation keeped
它可能非常简单,但我卡住了,不知道如何解决它。
我有两个实体:
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.Now;
public DateTime ModifiedDate { get; set; } = DateTime.Now;
public bool IsActive { get; set; } = true;
}
public class Vat : BaseEntity
{
public string Name { get; set; }
public decimal Rate { get; set; }
public Country Country { get; set; }
}
public class Country : BaseEntity
{
public string Name { get; set; }
public string Code { get; set; }
public ICollection<Vat> Vats { get; set; }
}
和定义:
public override void Configure(EntityTypeBuilder<Vat> builder)
{
base.Configure(builder);
builder.HasOne(e => e.Country).WithMany(e => e.Vats);
}
种子看起来像这样:
modelBuilder.Entity<Country>().HasData(
new Country { Id = 1, Code = "FR", Name = "France" },
new Country { Id = 2, Code = "DE", Name = "Germany" },
new Country { Id = 3, Code = "CZ", Name = "Czech" }
);
modelBuilder.Entity<Vat>().HasData(
new Vat { Id = 1, Name = "23%", Rate = 23, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 }, },
new Vat { Id = 2, Name = "8%", Rate = 8, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } },
new Vat { Id = 3, Name = "5%", Rate = 5, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } },
new Vat { Id = 4, Name = "0%", Rate = 0, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } }
);
但是当我播种增值税时,我收到了这个错误:
The seed entity for entity type 'Vat' cannot be added because it has the navigation 'Country' set. To seed relationships, add the entity seed to 'Vat' and specify the foreign key values {'CountryId'}.
知道什么是播种增值税实体并使其与国家/地区相关的正确方法吗?
感谢 Bogdan 它有效,但现在我在多对多关系中遇到了类似的问题:
public class Product : BaseEntity
{
public string Name { get; set; }
public string ShortName { get; set; }
public string Description { get; set; }
public decimal PriceMin { get; set; }
public decimal PriceMax { get; set; }
public decimal CostNetto { get; set; }
public ICollection<Vat> Vats { get; set; }
}
public class Vat : BaseEntity
{
public string Name { get; set; }
public decimal Rate { get; set; }
public Country Country { get; set; }
public ICollection<Product> Products { get; set; }
}
试过类似的东西:
var products = new List<object>()
{
new { Id=1, Name="Proszek Omo", Description="Proszek do prania Omo jest super", CostNetto=12m, PriceMin=14m, PriceMax=18m, ShortName="Omo", IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Vats=new List<object>{ new { VatsId = 1 } }, Tags=new List<object>{new {TagsId=1 }, new { TagsId = 3 } } }
};
modelBuilder.Entity<Product>().HasData(products);
但仍然出现此错误:
The seed entity for entity type 'Product' cannot be added because it has the navigation 'Vats' set. To seed relationships, add the entity seed to 'ProductVat (Dictionary<string, object>)' and specify the foreign key values {'ProductsId'}
要为具有导航属性的实体播种,您可以使用动态对象。
这种方式应该可行:
modelBuilder.Entity<Vat>().HasData(
new List<object>
{
new { Id = 1, Name = "23%", Rate = 23, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CountryId = 1 },
new { Id = 2, Name = "8%", Rate = 8, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CountryId = 1 },
...
}
);
在这种情况下,我们可以这样播种多对多关系:
var productVats = new List<object>()
{
new { ProductsId=1, VatsId=1},
new { ProductsId=1, VatsId=5}
};
modelBuilder.Entity<Product>().HasMany(p => p.Vats).WithMany(p => p.Products).UsingEntity(j => j.HasData(productVats));
它可能非常简单,但我卡住了,不知道如何解决它。 我有两个实体:
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.Now;
public DateTime ModifiedDate { get; set; } = DateTime.Now;
public bool IsActive { get; set; } = true;
}
public class Vat : BaseEntity
{
public string Name { get; set; }
public decimal Rate { get; set; }
public Country Country { get; set; }
}
public class Country : BaseEntity
{
public string Name { get; set; }
public string Code { get; set; }
public ICollection<Vat> Vats { get; set; }
}
和定义:
public override void Configure(EntityTypeBuilder<Vat> builder)
{
base.Configure(builder);
builder.HasOne(e => e.Country).WithMany(e => e.Vats);
}
种子看起来像这样:
modelBuilder.Entity<Country>().HasData(
new Country { Id = 1, Code = "FR", Name = "France" },
new Country { Id = 2, Code = "DE", Name = "Germany" },
new Country { Id = 3, Code = "CZ", Name = "Czech" }
);
modelBuilder.Entity<Vat>().HasData(
new Vat { Id = 1, Name = "23%", Rate = 23, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 }, },
new Vat { Id = 2, Name = "8%", Rate = 8, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } },
new Vat { Id = 3, Name = "5%", Rate = 5, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } },
new Vat { Id = 4, Name = "0%", Rate = 0, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Country = new Country { Id = 1 } }
);
但是当我播种增值税时,我收到了这个错误:
The seed entity for entity type 'Vat' cannot be added because it has the navigation 'Country' set. To seed relationships, add the entity seed to 'Vat' and specify the foreign key values {'CountryId'}.
知道什么是播种增值税实体并使其与国家/地区相关的正确方法吗?
感谢 Bogdan 它有效,但现在我在多对多关系中遇到了类似的问题:
public class Product : BaseEntity
{
public string Name { get; set; }
public string ShortName { get; set; }
public string Description { get; set; }
public decimal PriceMin { get; set; }
public decimal PriceMax { get; set; }
public decimal CostNetto { get; set; }
public ICollection<Vat> Vats { get; set; }
}
public class Vat : BaseEntity
{
public string Name { get; set; }
public decimal Rate { get; set; }
public Country Country { get; set; }
public ICollection<Product> Products { get; set; }
}
试过类似的东西:
var products = new List<object>()
{
new { Id=1, Name="Proszek Omo", Description="Proszek do prania Omo jest super", CostNetto=12m, PriceMin=14m, PriceMax=18m, ShortName="Omo", IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Vats=new List<object>{ new { VatsId = 1 } }, Tags=new List<object>{new {TagsId=1 }, new { TagsId = 3 } } }
};
modelBuilder.Entity<Product>().HasData(products);
但仍然出现此错误:
The seed entity for entity type 'Product' cannot be added because it has the navigation 'Vats' set. To seed relationships, add the entity seed to 'ProductVat (Dictionary<string, object>)' and specify the foreign key values {'ProductsId'}
要为具有导航属性的实体播种,您可以使用动态对象。 这种方式应该可行:
modelBuilder.Entity<Vat>().HasData(
new List<object>
{
new { Id = 1, Name = "23%", Rate = 23, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CountryId = 1 },
new { Id = 2, Name = "8%", Rate = 8, IsActive = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CountryId = 1 },
...
}
);
在这种情况下,我们可以这样播种多对多关系:
var productVats = new List<object>()
{
new { ProductsId=1, VatsId=1},
new { ProductsId=1, VatsId=5}
};
modelBuilder.Entity<Product>().HasMany(p => p.Vats).WithMany(p => p.Products).UsingEntity(j => j.HasData(productVats));