Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Primary Key field on MVC class

I am new to MVC and C#. I just stumbled on it and found it interesting. I encountered an issue which will not allow me proceed. Here 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 created the controler and the view thereafter.

And for this, I keep got this error:

One or more validation errors were detected during model generation:

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 google that the answers were to add [Key] over the public int AccountTypeID { get; set; } so it could look like this:

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

But no result until now. Note: I am using MVC 4

like image 240
Peter Avatar asked Apr 19 '12 21:04

Peter


People also ask

How is primary key defined in Model class?

For defining the primary key, it provides HasKey() method. The Fluent API takes priority over the Data Annotation attributes. To specify the mappings using Code First Fluent API, we have to override the OnModelCreating() method. The OnModelCreating() method is called before the models are created.

How do I set primary key in Entity Framework?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

How can add foreign key in MVC Model?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.


1 Answers

Description

Entity Framework CodeFirst recognize the key, by default, by name. Valid names are Id or <YourClassName>Id.

Your property should named Id or AccountTypesId

Another way is to use the ModelBuilder to specify 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 Information

  • Entity Framework Code First Tutorial
like image 101
dknaack Avatar answered Sep 30 '22 22:09

dknaack