0
votes

I am using Entity Framework 5 to access data from SQL Server 2008 R2 in an MVC 4 Application. This is my 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; }
}

My exact Error is "EntityType 'Attribute' has no key defined. Define the key for this EntityType.". In my other entity-classes this was resolved by adding the [Key]-parameter

The variables have exactly the same names as in the database.

Thanks in adavance for your help.

Stack Trace:

[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
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.InternalSet1.get_InternalContext() +38 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +84 System.Linq.Queryable.Select(IQueryable1 source, Expression1 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
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
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
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, 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
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, 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
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, 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

1
Try giving it a name like AttIdMurtaza Munshi
That didn't help me either. Even calling it T_AttributeId (so tablename + Id as somewhere mentionend) didn't help. Thanks thoughAsator
Can you please post the references of the cs fileMurtaza Munshi
Try adding the [Required] attribute below key attributeMurtaza Munshi
Required didn't fix it either using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text;Asator

1 Answers

0
votes

Deleting the Database and going over to Code-First solved the problem for me. Thanks for your help.