【发布时间】:2016-05-26 06:07:16
【问题描述】:
我需要在 DbContext 中命令创建 DbSet。因为:
- 我注意到 Seed 方法存在问题(外键违规)。
- 参考这个我无法解决的Reading MaxLength from MetadataWorkspace。我需要确定每个确切实体的数量。
顺便说一句,DbSets已经在DbContext中排序了,但是创建迁移时似乎没有效果。
在我的 DbContext 中:
public class CmsDbContext : DbContext
{
// Basic
public DbSet<Country> Countries { get; set; }
public DbSet<District> Districts { get; set; }
public DbSet<Province> Provinces { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Area> Areas { get; set; }
public DbSet<CustomerType> CustomerTypes { get; set; }
public DbSet<IdType> IdTypes { get; set; }
public DbSet<IncomeType> IncomeTypes { get; set; }
public DbSet<OutcomeType> OutcomeTypes { get; set; }
public DbSet<PaymentType> PaymentTypes { get; set; }
}
如您所见,国家是第一个。 发行后
add-migration initial
DbSet 的顺序不是我想要的:
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Area",
c => new
{
AreaId = c.Int(nullable: false, identity: true),
CityId = c.Int(nullable: false),
AreaArabicName = c.String(nullable: false, maxLength: 35, unicode: false),
AreaEnglishName = c.String(nullable: false, maxLength: 35, unicode: false),
AddedDate = c.DateTime(),
ModifiedDate = c.DateTime(),
})
.PrimaryKey(t => t.AreaId)
.ForeignKey("dbo.City", t => t.CityId)
.Index(t => t.CityId);
CreateTable(
"dbo.City",
c => new
{
CityId = c.Int(nullable: false, identity: true),
ProvinceId = c.Int(nullable: false),
CityArabicName = c.String(nullable: false, maxLength: 35, unicode: false),
CityEnglishName = c.String(nullable: false, maxLength: 35, unicode: false),
AddedDate = c.DateTime(),
ModifiedDate = c.DateTime(),
})
.PrimaryKey(t => t.CityId)
.ForeignKey("dbo.Province", t => t.ProvinceId)
.Index(t => t.ProvinceId);
等等……
请注意,Country 不是要创建的第一个表。
当我发布时:
update-database
数据库已创建,表也已创建,但种子方法没有。我遇到了异常:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Area_dbo.City_CityId".
如果我停止向 City 播种,就不会发生这种情况。
我希望问题现在很清楚。
这是城市的种子
class CityDefaultData
{
private readonly string[,] _cities = new string[,]
{
{"1", "الرياض", "Riyadh"},
{"2", "جدة", "Jeddah"},
{"3", "الدمام", "Dammam"},
{"4", "بريدة", "Buraidah"}
};
private readonly List<City> _newCitiess = new List<City>();
public List<City> GetDefaultCity()
{
for (var i = 0; i <= _cities.GetUpperBound(0); i++)
{
_newCitiess.Add(new City()
{
ProvinceId = Convert.ToInt32(_cities[i, 0]),
CityArabicName = _cities[i, 1],
CityEnglishName = _cities[i, 2]
});
}
return _newCitiess;
}
}
区域的种子:
class AreaDefaultData
{
private readonly string[,] _areas = new string[,]
{
{"1", "السلام", "Salam"},
{"1", "العليا", "Olayya"},
{"1", "الروابي", "Rawabi"},
{"1", "القدس", "Quds"},
{"1", "المنار", "Manar"},
{"2", "السلام", "Salam"},
{"2", "العليا", "Olayya"},
{"2", "الروابي", "Rawabi"},
{"2", "القدس", "Quds"},
{"2", "المنار", "Manar"}
};
private readonly List<Area> _newAreas = new List<Area>();
public List<Area> GetDefaultArea()
{
for (var i = 0; i <= _areas.GetUpperBound(0); i++)
{
_newAreas.Add(new Area()
{
CityId = Convert.ToInt32(_areas[i, 0]),
AreaArabicName = _areas[i, 1],
AreaEnglishName = _areas[i, 2]
});
}
return _newAreas;
}
}
这是我的 DbInitialize
class DbInitializer
{
public void DataSeed(CmsDbContext context)
{
// Country
List<Country> coutry =
new CountryDefaultData().GetDefaultCountry();
coutry.ForEach(e => context.Countries.Add(e));
context.Countries.AddOrUpdate();
// District
List<District> district =
new DistrictDefaultData().GetDefaultDistrict();
district.ForEach(e => context.Districts.Add(e));
context.Districts.AddOrUpdate();
// Province
List<Province> province =
new ProvinceDefaultData().GetDefaultProvince();
province.ForEach(e => context.Provinces.Add(e));
context.Provinces.AddOrUpdate();
// City
List<City> city =
new CityDefaultData().GetDefaultCity();
city.ForEach(e => context.Cities.Add(e));
context.Cities.AddOrUpdate();
// Area
List<Area> area =
new AreaDefaultData().GetDefaultArea();
area.ForEach(e => context.Areas.Add(e));
context.Areas.AddOrUpdate();
// Id Type
List<IdType> idType =
new IdTypeDefultData().GetDefaultIdTypes();
idType.ForEach(e => context.IdTypes.Add(e));
context.IdTypes.AddOrUpdate();
// Customer Type
List<CustomerType> customerType =
new CustomerTypeDefaultData().GetDefaultCustomerType();
customerType.ForEach(e => context.CustomerTypes.Add(e));
context.CustomerTypes.AddOrUpdate();
// Income Type
List<IncomeType> incomeType =
new IncomeTypeDefaultData().GetDefaultIncomeType();
incomeType.ForEach(e => context.IncomeTypes.Add(e));
context.IncomeTypes.AddOrUpdate();
// Outcome Type
List<OutcomeType> outcomeType =
new OutcomeTypeDefaultData().GetDefaultOutcomeType();
outcomeType.ForEach(e => context.OutcomeTypes.Add(e));
context.OutcomeTypes.AddOrUpdate();
// Payment Type
List<PaymentType> paymentType =
new PaymentTypeDefaultData().GetDefaultPaymentType();
paymentType.ForEach(e => context.PaymentTypes.Add(e));
context.PaymentTypes.AddOrUpdate();
}
}
最后:
internal sealed class Configuration : DbMigrationsConfiguration<CMS.Model.Domain.CmsDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(CMS.Model.Domain.CmsDbContext context)
{
DbInitializer all = new DbInitializer();
all.DataSeed(context);
}
}
【问题讨论】:
-
我不知道你在说什么。请显示重现问题的代码。
-
@Gert Arnold,感谢您的关心。我更新了问题。
-
Seed 方法会导致问题,这就是您应该展示的内容。其余的无关紧要。如果数据库创建得很好(就是这样),为什么还要担心
CreateTable语句的顺序呢? -
您似乎认为创建数据库表后,立即对其进行种子化。但是 EF 怎么会知道怎么做呢?首先创建数据库,即整个 数据库。 然后执行种子方法。问题是您的种子数据没有正确的相互引用,特别是:
Areas 没有City引用。 -
我和@GertArnold 在一起。附带说明一下,您的
AddOrUpdate调用无济于事。
标签: c# entity-framework dbcontext