2015-06-02 110 views
0

我正在使用实体框架5在MVC 4应用程序中访问SQL Server 2008 R2中的数据。这是我的T_Attribute类:“的EntityType‘属性’没有定义键定义此的EntityType关键。”即使定义了[键],EntityType'属性'也没有定义密钥

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; } 
} 

我确切的错误是。在我的其他实体类中,这是通过添加[Key]参数来解决的。这些变量的名称与数据库中的名称完全相同。

非常感谢您的帮助。

堆栈跟踪:

[ModelValidationException:一个或多个验证错误检测 模型生成期间:

\ tSystem.Data.Entity.Edm.EdmEntityType:的EntityType '属性' 具有 没有定义密钥。定义此EntityType的关键字。 \ tSystem.Data.Entity.Edm.EdmEntityType:Name:指定的名称是 不允许:'__DynamicallyInvokableAttribute'。 \ tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet 'T_Attribute1'基于没有定义键的'Attribute'类型。 ]
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel 模型,XmlWriter的作家)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 internalConte XT)143
System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input) +171
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
1.Initialize()137
System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() +38 System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() + 84 System.Linq.Queryable.Select(IQueryable的1 source, Expression 1选择器)66个
HP.GPS.ClearingCenter.Application.Products.CategoryDataService.GetAllCategories() 在FilepathToSolution。应用\产品\ CategoryDataService.cs:22 HP .GPS.ClearingCenter.Web.ViewModels.Home.IndexViewModel.Initialize() in FilepathToSolution .Web \ ViewModels \ Home \ Index ViewModel.cs:27
.Web.Controllers.HomeController.Search()在FilepathToSolution名.web \控制器\ HomeController.cs:22 lambda_method(封闭,ControllerBase,对象[])101
系统。 Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[]参数)59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary的参数)60
System.Web.Mvc 。Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary的2 parameters) +50
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.WrappedAsyncResult 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
1.End() +139 System.Web.Mvc.Async .AsyncResultWrapper.End(IAsyncResult的asyncResult,对象标签)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.As ync.WrappedAsyncResult 1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
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
1.End()151
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标签)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.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.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
1.End()151
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 result)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 9651188 System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔& completedSynchronously)155

+0

尝试给它一个名字,如AttId –

+0

这也没有帮助我。即使称它为T_AttributeId(所以tablename + Id在某处提到)并没有帮助。感谢 – Asator

+0

您可以发布cs文件的参考 –

回答

0

删除数据库和去那边代码首先解决了这个问题对我来说。谢谢你的帮助。

相关问题