我用谷歌搜索了这个,但没有得到任何答案。
我以一种简单的方式使用Entity Framework。我正在尝试将记录添加到Memberproduct表中。但是我得到了一个没有意义的例外。
这里有什么想法吗?
MemberProduct类:
public class MemberProduct :ISaleable
{
public void ProcessSale()
{
throw new NotImplementedException();
}
private int id { get; set; }
private string productName { get; set; }
private decimal price { get; set; }
private TaxClass taxClass { get; set; }
private int quantity { get; set; }
private Member memberAssociation { get; set; }
public TaxClass TaxClass
{
get
{
return this.taxClass;
}
set
{
this.taxClass = value;
}
}
public int Quantity
{
get
{
return this.quantity;
}
set
{
this.quantity = value;
}
}
public string ProductName
{
get
{
return this.productName;
}
set
{
this.productName = value;
}
}
public decimal Price
{
get
{
return this.price;
}
set
{
this.price = value;
}
}
public Member MemberAssociation
{
get
{
return this.memberAssociation;
}
set
{
this.memberAssociation = value;
}
}
public int ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
堆栈追踪:
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.IdKeyDiscoveryConventionImpl.MatchKeyProperty(EdmEntityType entityType, IEnumerable`1 primitiveProperties)
at System.Data.Entity.ModelConfiguration.Conventions.KeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch[TEdmDataModelItem](TEdmDataModelItem item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item)
at System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
at System.Data.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes)
at System.Data.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item)
at System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
at System.Data.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces)
at System.Data.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch()
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at Nautix_EPOS.Controllers.HomeController.Index() in C:\sites\EPOS\Nautix EPOS\Nautix EPOS\Controllers\HomeController.cs:line 19
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
答案 0 :(得分:9)
我可以重复你的问题。您的MemberProduct
具有两个 Id
属性,具有不同的大小写:
public class MemberProduct
{
public int Id { get; set; }
public int ID { get; set; }
}
EF代码首先在映射期间使用约定。其中一个约定是它将名为Id
或TypenameId
的属性视为主键(如果不使用Key
属性或自定义映射)并且因为它执行属性名称比较case不敏感地抛出异常。
删除其中一个属性,它应该可以正常工作。
答案 1 :(得分:4)
如果它对任何人都有用:
从存储过程使用EF填充模型时收到此错误。存储过程返回多个列,其中两列具有相同的别名/名称。
答案 2 :(得分:1)
我怀疑EF对两个ID属性感到困惑。
将您的私人资源更改为字段;应该解决它。
答案 3 :(得分:0)
在初始播种时错误地设置实体状态信息时出现此序列错误。只需确保正确设置对象图的状态。我的问题是播种数据(不是在实际的应用程序运行期间)...无论如何,如果它们在运行更新时遇到序列错误(例如“序列包含多个匹配元素”)将帮助任何人,这是我的答案-database命令用于迁移。虽然你在种子方法之外得到这个错误,但我认为你的答案可能在于实体状态正确设置了对象图。
Entity Framework Code First AddOrUpdate method insert Duplicate values
要获得有关正确设置实体对象图的帮助,请参阅以下博客文章,这是我在正确设置对象状态时发现的最佳信息: