EntityType 'Attribute' 没有定义键,即使定义了 [key]
EntityType 'Attribute' has no key defined even though [key] is defined
我正在使用 Entity Framework 5 在 MVC 4 应用程序中访问来自 SQL Server 2008 R2 的数据。这是我的 T_Attribute class:
public partial class T_Attribute
{
public T_Attribute()
{
this.T_Characteristic = new HashSet<T_Characteristic>();
this.T_Category = new HashSet<T_Category>();
}
[Key]
public int AttributeId { get; set; }
public string Descr { get; set; }
public Nullable<bool> HasCharacteristics { get; set; }
public virtual ICollection<T_Characteristic> T_Characteristic { get; set; }
public virtual ICollection<T_Category> T_Category { get; set; }
}
我的确切错误是 "EntityType 'Attribute' has no key defined. Define the key for this EntityType."。在我的其他实体中-classes,这是通过添加 [Key]-parameter
解决的
变量与数据库中的名称完全相同。
预先感谢您的帮助。
堆栈跟踪:
[ModelValidationException: One or more validation errors were detected
during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Attribute' has
no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: Name: The specified name is
not allowed: '__DynamicallyInvokableAttribute'.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet
'T_Attribute1' is based on type 'Attribute' that has no keys defined.
]
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel
model, XmlWriter writer) +114
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel
model) +77
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest
providerManifest, DbProviderInfo providerInfo) +218
System.Data.Entity.DbModelBuilder.Build(DbConnection
providerConnection) +107
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext
internalContext) +143
System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) +171<br>
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
+498 System.Data.Entity.Internal.InternalContext.Initialize() +31 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type
entityType) +39<br>
System.Data.Entity.Internal.Linq.InternalSet
1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
+38 System.Data.Entity.Infrastructure.DbQuery
1.System.Linq.IQueryable.get_Provider()
+84 System.Linq.Queryable.Select(IQueryable1 source, Expression
1 selector) +66
HP.GPS.ClearingCenter.Application.Products.CategoryDataService.GetAllCategories()
in FilepathToSolution.Application\Products\CategoryDataService.cs:22
HP.GPS.ClearingCenter.Web.ViewModels.Home.IndexViewModel.Initialize()
in FilepathToSolution.Web\ViewModels\Home\IndexViewModel.cs:27
Solution.Web.Controllers.HomeController.Search() in FilepathToSolution.Web\Controllers\HomeController.cs:22 lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary2 parameters) +434<br>
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary
2
parameters) +60
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary2
parameters) +50<br>
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
+75 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult
_) +44 System.Web.Mvc.Async.WrappedAsyncResult1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +102<br>
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
asyncResult) +49<br>
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
+126 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
+323 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult
1.End()
+139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult
asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2a.b__20()
+68 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult
asyncResult) +184 System.Web.Mvc.Async.WrappedAsyncResult1.End()
+136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56<br>
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
asyncResult) +40<br>
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult
asyncResult) +40<br>
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult
ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult
ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151<br>
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59<br>
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult
asyncResult) +39<br>
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
asyncResult) +39<br>
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult
asyncResult) +45<br>
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult
ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+9651188 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
删除数据库并转到代码优先解决了我的问题。感谢您的帮助。
我正在使用 Entity Framework 5 在 MVC 4 应用程序中访问来自 SQL Server 2008 R2 的数据。这是我的 T_Attribute class:
public partial class T_Attribute
{
public T_Attribute()
{
this.T_Characteristic = new HashSet<T_Characteristic>();
this.T_Category = new HashSet<T_Category>();
}
[Key]
public int AttributeId { get; set; }
public string Descr { get; set; }
public Nullable<bool> HasCharacteristics { get; set; }
public virtual ICollection<T_Characteristic> T_Characteristic { get; set; }
public virtual ICollection<T_Category> T_Category { get; set; }
}
我的确切错误是 "EntityType 'Attribute' has no key defined. Define the key for this EntityType."。在我的其他实体中-classes,这是通过添加 [Key]-parameter
解决的变量与数据库中的名称完全相同。
预先感谢您的帮助。
堆栈跟踪:
[ModelValidationException: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Attribute' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntityType: Name: The specified name is not allowed: '__DynamicallyInvokableAttribute'. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'T_Attribute1' is based on type 'Attribute' that has no keys defined. ]
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer) +114
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel model) +77
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +218
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +107
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +143
System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) +171<br> System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +498 System.Data.Entity.Internal.InternalContext.Initialize() +31 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39<br> System.Data.Entity.Internal.Linq.InternalSet
1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() +38 System.Data.Entity.Infrastructure.DbQuery
1.System.Linq.IQueryable.get_Provider() +84 System.Linq.Queryable.Select(IQueryable1 source, Expression
1 selector) +66
HP.GPS.ClearingCenter.Application.Products.CategoryDataService.GetAllCategories() in FilepathToSolution.Application\Products\CategoryDataService.cs:22 HP.GPS.ClearingCenter.Web.ViewModels.Home.IndexViewModel.Initialize() in FilepathToSolution.Web\ViewModels\Home\IndexViewModel.cs:27
Solution.Web.Controllers.HomeController.Search() in FilepathToSolution.Web\Controllers\HomeController.cs:22 lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +434<br> System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +60
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +50<br> System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +75 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult _) +44 System.Web.Mvc.Async.WrappedAsyncResult1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102<br> System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49<br> System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +126 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +323 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2a.b__20() +68 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +184 System.Web.Mvc.Async.WrappedAsyncResult1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56<br> System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40<br> System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40<br> System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151<br> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59<br> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39<br> System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39<br> System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45<br> System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
删除数据库并转到代码优先解决了我的问题。感谢您的帮助。