MVC class primary key field creation

asp.net-mvc asp.net-mvc-4 c# entity-framework

Question

Both MVC and C# are new to me. I simply ran over it and thought it was fascinating. I ran across a problem that will prevent me from moving on. This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; 

namespace MyHotel.Models
{
    public class AccountTypes
    {
        public int AccountTypeID { get; set; }
        public string AccountTypeName { get; set; }
    }
}

I then developed the controller and the view.

And as a result, I continue to get the following error:

During the model creation process, one or more validation problems were found:

System.Data.Edm.EdmEntityType: : EntityType 'AccountTypes' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet "AccountTypes" is based on type "AccountTypes" that has no keys defined.

I used Google to find the answers.[Key] more thanpublic int AccountTypeID { get; set; } Thus, it may seem as follows:

namespace MyHotel.Models
{
    public class AccountTypes
    {
        [Key]
        public int AccountTypeID { get; set; }
        public string AccountTypeName { get; set; }
    }
}

But up till today, no outcome. I am using MVC 4

1
14
11/12/2014 2:06:13 PM

Accepted Answer

Description

Code for Entity Framework Recognize the key first by name, which is the default. proper names areId or <YourClassName>Id .

You need to name your propertyId or AccountTypesId

There is also the usage ofModelBuilder for describing the key.

Sample

public class MyDbContext : DbContext
{
    public DbSet<AccountTypes> AccountTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AccountTypes>.HasKey(x => x.AccountTypeID);
        base.OnModelCreating(modelBuilder);
    }
}

Mode-specific data

20
4/19/2012 9:18:49 PM

Popular Answer

To identify the key field, try using the [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] property.

EntityKeyPropert=false would be appropriate for the standard field.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow