C# DbMigration种子序列包含多个元素
我有以下课程C# DbMigration种子序列包含多个元素,c#,asp.net-mvc,entity-framework,code-first,C#,Asp.net Mvc,Entity Framework,Code First,我有以下课程 public class Lookup { public int Id{get;set;} public string Name{get;set;} public int Order{get;set;} } public class CatalogType:Lookup // this was added on Add-migration "Second" { } public class Catalog:Lookup { public int
public class Lookup
{
public int Id{get;set;}
public string Name{get;set;}
public int Order{get;set;}
}
public class CatalogType:Lookup // this was added on Add-migration "Second"
{
}
public class Catalog:Lookup
{
public int CatalogTypeId{get;set;} // this was added on add-migration "Second"
public CatalogType CatalogType{get;set;}
}
我已经在数据库中的表查找中有了数据,这些数据表示一组查找类,如性别、婚姻状况、目录等。查找表包含一行,其中包含目录使用的Name=“General”
(即Discriminator field=“catalog”)
在Seed函数内的配置文件中,我编写了以下代码
context.Lookups.AddOrUpdate(p => new { p.Name, **p.GetType().FullName** },
new CatalogType
{
Name = "General",
IsActive = true,
Order = 1,
},
new CatalogType
{
Name = "Custom",
IsActive = true,
Order = 2,
});
context.SaveChanges();
我的问题:我首先尝试了context.Lookups.AddOrUpdate(p=>p.Name)
,当我尝试进行updatedatabase
时,迁移失败“序列包含多个元素
”
然后我尝试使用p.GetType().Name
错误是:
匿名类型不能有多个同名属性
然后我尝试使用p.GetType().FullName
,在执行updatedatabase
命令时,出现以下错误:
属性表达式'p=>newf_u匿名类型18`2(名称=
p、 名称,FullName=p.GetType().FullName)“”无效。表情
应表示一个属性:C#::“t=>t.MyProperty”VB.Net:
“函数(t)t.MyProperty”。指定多个属性时,请使用
匿名类型:C#:'t=>new{t.MyProperty1,t.MyProperty2}'
Net:'函数(t)使用{t.MyProperty1,t.MyProperty2}新建'
我知道这个问题是因为查找表已经包含了Name=“General”
,但是如何告诉EntityFramework在尝试添加或更新方法时考虑列鉴别器呢
换句话说,我可能有两个不同对象的相同数据,我想添加关于添加迁移的数据,如果我有例如red car、red door和我想添加例如red apple的数据,如何实现这一点?它不允许我在我目前的情况下,如何解决这个问题
希望我对这个问题的解释清楚。试试这个:
//but two lines below in "OnModelCreating" method in your Context
modelBuilder.Entity<Lookup>().Map<Catalog>(m => m.Requires("IsCatalog").HasValue(true));
modelBuilder.Entity<Lookup>().Map<CatalogType>(m =>m.Requires("IsCatalog").HasValue(false));
// then :
context.Lookups.AddOrUpdate(p => new { p.Name , p.IsCatalog},
new CatalogType
{
Name = "General",
IsActive = true,
Order = 1,
},
new CatalogType
{
Name = "Custom",
IsActive = true,
Order = 2,
});
//context.SaveChanges(); //if you used base.OnModelCreating(modelBuilder);
base.OnModelCreating(modelBuilder); // then you don't need to save
//但在上下文中的“OnModelCreating”方法中有两行
modelBuilder.Entity().Map(m=>m.Requires(“IsCatalog”).HasValue(true));
modelBuilder.Entity().Map(m=>m.Requires(“IsCatalog”).HasValue(false));
//然后:
context.Lookups.AddOrUpdate(p=>new{p.Name,p.IsCatalog},
新目录类型
{
Name=“General”,
IsActive=true,
订单=1,
},
新目录类型
{
Name=“自定义”,
IsActive=true,
订单=2,
});
//SaveChanges()//如果使用base.OnModelCreating(modelBuilder);
base.OnModelCreating(modelBuilder);//那你就不需要存钱了
在搜索了这个主题后,我得到了这个结果
希望这将帮助其他像我一样遇到同样问题的人这个解决方案将在查找表中创建一列IsCatalog,对吗?我正在尝试使用已经存在的列鉴别器Yes,如果您没有像我使用map那样指定鉴别器列(您还可以指定类型和值)。。。它将创建自己的鉴别器(默认鉴别器),无论如何尝试使用新的{p.Name,p.Discriminator}。。注意:默认鉴别器类型:bit(true/false)我尝试了您的代码,这里的问题我无法添加此映射条件,因为Catalog是TPC,CatalogType是TPH,EF将不接受此实现问题不在TPH或TPC中,我想做的是结合TPH和TPC,两者都是从同一个类查找中继承的,这在谷歌搜索itBINGO后实现映射条件时是不允许的。编辑你的答案,我会给你奖励