【发布时间】:2014-11-09 07:22:38
【问题描述】:
我有以下课程
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;}
}
并且我已经在表 Lookups 中的数据库中有数据,这些数据表示一组查找类,如性别、婚姻状况、目录等,并且 Lookups 表包含目录使用的 Name="General" 行(即鉴别器字段="目录")
在种子函数里面的配置文件中我写了这段代码
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),当我尝试制作update-database时,迁移失败“sequence contains more than one element”
然后我尝试使用p.GetType().Name 错误是:
一个匿名类型不能有多个同名的属性。
然后我尝试使用p.GetType().FullName 并在执行update-database 命令时出现以下错误:
属性表达式 'p => new f__AnonymousType18`2(Name = p.Name, FullName = p.GetType().FullName)' 无效。表达方式 应该代表一个属性: C#: 't => t.MyProperty' VB.Net: '函数(t)t.MyProperty'。当指定多个属性时,使用 匿名类型:C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'。
我知道问题是因为 Lookups 表已经包含 Name="General",但是如何告诉 EntityFramework 在尝试 AddOrUpdate 方法时考虑列鉴别器?
换句话说,我可能有 2 个不同对象的相同数据,并且我想在添加迁移时添加数据,如果我有例如红色汽车、红色门并且我想添加红色苹果,如何实现这一点?在我目前的情况下它不允许我,如何解决这个问题?
希望我对这个问题的解释很清楚。
【问题讨论】:
标签: c# asp.net-mvc entity-framework code-first