我正在使用Entity Framework 5在MVC 4应用程序中访问SQL Server 2008 R2中的数据。这是我的T_Attribute类:
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; }
}
我的确切错误是&#34;实体类型&#39;属性&#39;没有定义键。定义此EntityType的键。&#34;。在我的其他实体类中,通过添加[Key]参数
解决了这个问题变量与数据库中的名称完全相同。
谢谢你的帮助。
堆栈追踪:
[ModelValidationException:检测到一个或多个验证错误 在模型生成期间:
\ tSystem.Data.Entity.Edm.EdmEntityType :: EntityType&#39;属性&#39;具有 没有键定义。定义此EntityType的键。 \ tSystem.Data.Entity.Edm.EdmEntityType:Name:指定的名称是 不允许:&#39; __ DynamicallyInvokableAttribute&#39;。 \ tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet &#39; T_Attribute1&#39;基于类型&#39;属性&#39;没有定义键。 ]
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model,XmlWriter writer)+114
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel 型号)+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
1.Initialize()+137
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +498 System.Data.Entity.Internal.InternalContext.Initialize() +31 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet
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选择器)+66
HP.GPS.ClearingCenter.Application.Products.CategoryDataService.GetAllCategories() 在 FilepathToSolution .Application \ Products \ CategoryDataService.cs:22 HP.GPS.ClearingCenter.Web.ViewModels.Home.IndexViewModel.Initialize() 在 FilepathToSolution .Web \ ViewModels \ Home \ IndexViewModel.cs:27
解决方案 .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 []参数)+59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters) +434
2 参数)+60
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary2 parameters) +50
1.b__7(IAsyncResult) _)+44 System.Web.Mvc.Async.WrappedAsyncResult
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +75 System.Web.Mvc.Async.<>c__DisplayClass81.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
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
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的 asyncResult)+50
System.Web.Mvc.Async&LT;&GT; c__DisplayClass2a.b__20() +68 System.Web.Mvc.Async。&lt;&gt; 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
1.End()+151
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+44 System.Web.Mvc.Async&LT;&GT; c__DisplayClass4.b__3(IAsyncResult的 ar)+47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151
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.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, 对象标签)+40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult) 结果)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:0)
删除数据库并转到Code-First解决了我的问题。谢谢你的帮助。