EntityType has no key defined error











up vote
115
down vote

favorite
21












Controller:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
public class studentsController : Controller
{
//
// GET: /students/

public ActionResult details()
{
int id = 16;
studentContext std = new studentContext();
student first = std.details.Single(m => m.RollNo == id);
return View(first);
}

}
}


DbContext Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
public class studentContext : DbContext
{
public DbSet<student> details { get; set; }
}
}


Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
public int RollNo;
public string Name;
public string Stream;
public string Div;
}
}


Database table:



CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)


In global.asax.cs



Database.SetInitializer<MvcApplication1.Models.studentContext>(null);


The above code lists all the classes I am working on. Upon running my application am receiving the error:




"One or more validation errors were detected during model generation" along with "Entity type has no key defined".











share|improve this question




















  • 2




    Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
    – Tosh
    Jun 30 '15 at 18:25















up vote
115
down vote

favorite
21












Controller:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
public class studentsController : Controller
{
//
// GET: /students/

public ActionResult details()
{
int id = 16;
studentContext std = new studentContext();
student first = std.details.Single(m => m.RollNo == id);
return View(first);
}

}
}


DbContext Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
public class studentContext : DbContext
{
public DbSet<student> details { get; set; }
}
}


Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
public int RollNo;
public string Name;
public string Stream;
public string Div;
}
}


Database table:



CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)


In global.asax.cs



Database.SetInitializer<MvcApplication1.Models.studentContext>(null);


The above code lists all the classes I am working on. Upon running my application am receiving the error:




"One or more validation errors were detected during model generation" along with "Entity type has no key defined".











share|improve this question




















  • 2




    Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
    – Tosh
    Jun 30 '15 at 18:25













up vote
115
down vote

favorite
21









up vote
115
down vote

favorite
21






21





Controller:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
public class studentsController : Controller
{
//
// GET: /students/

public ActionResult details()
{
int id = 16;
studentContext std = new studentContext();
student first = std.details.Single(m => m.RollNo == id);
return View(first);
}

}
}


DbContext Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
public class studentContext : DbContext
{
public DbSet<student> details { get; set; }
}
}


Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
public int RollNo;
public string Name;
public string Stream;
public string Div;
}
}


Database table:



CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)


In global.asax.cs



Database.SetInitializer<MvcApplication1.Models.studentContext>(null);


The above code lists all the classes I am working on. Upon running my application am receiving the error:




"One or more validation errors were detected during model generation" along with "Entity type has no key defined".











share|improve this question















Controller:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
public class studentsController : Controller
{
//
// GET: /students/

public ActionResult details()
{
int id = 16;
studentContext std = new studentContext();
student first = std.details.Single(m => m.RollNo == id);
return View(first);
}

}
}


DbContext Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
public class studentContext : DbContext
{
public DbSet<student> details { get; set; }
}
}


Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
public int RollNo;
public string Name;
public string Stream;
public string Div;
}
}


Database table:



CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)


In global.asax.cs



Database.SetInitializer<MvcApplication1.Models.studentContext>(null);


The above code lists all the classes I am working on. Upon running my application am receiving the error:




"One or more validation errors were detected during model generation" along with "Entity type has no key defined".








c# .net entity-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 28 '17 at 10:53









Balagurunathan Marimuthu

2,27341632




2,27341632










asked Nov 25 '13 at 21:16









Coder

2,37231125




2,37231125








  • 2




    Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
    – Tosh
    Jun 30 '15 at 18:25














  • 2




    Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
    – Tosh
    Jun 30 '15 at 18:25








2




2




Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
– Tosh
Jun 30 '15 at 18:25




Check the repository file. All the solutions in this page are amazing but in my case, I did everything right but forgot to declare the table as DbSet and add it to the modelbuilder.configurations.
– Tosh
Jun 30 '15 at 18:25












11 Answers
11






active

oldest

votes

















up vote
139
down vote



accepted










The Model class should be changed to :



using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
[Key]
public int RollNo { get; set; }

public string Name { get; set; }

public string Stream { get; set; }

public string Div { get; set; }
}
}





share|improve this answer





















  • my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
    – Mihai Bratulescu
    May 29 '14 at 9:31






  • 3




    Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
    – alex
    May 30 '14 at 11:10






  • 1




    And in case you are using it as a model in MVC, don't forget to rebuild!
    – RoJaIt
    Aug 2 at 12:27


















up vote
86
down vote














  1. Make sure public members of student class are defined as properties w/ {get; set;} (yours are public variables - a common mistake).


  2. Place a [Key] annotation on top of your chosen property.







share|improve this answer






























    up vote
    67
    down vote













    There are several reasons this can happen. Some of these I found here, others I discovered on my own.




    • If the property is named something other than Id, you need to add the [Key] attribute to it.

    • The key needs to be a property, not a field.

    • The key needs to be public

    • The key needs to be a CLS-compliant type, meaning unsigned types like uint, ulong etc. are not allowed.

    • This error can also be caused by configuration mistakes.






    share|improve this answer



















    • 3




      I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
      – JohnFx
      Sep 8 '16 at 21:45






    • 1




      @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
      – jeremysawesome
      Feb 9 '17 at 6:35










    • In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
      – Angela Amarapala
      Jul 18 '17 at 7:47


















    up vote
    19
    down vote













    i know that this post is late but
    this solution helped me:



        [Key]
    [Column(Order = 0)]
    public int RoleId { get; set; }


    added [Column(Order = 0)]
    after [Key]
    can be added by increment by 1:



        [Key]
    [Column(Order = 1)]
    public int RoleIdName { get; set; }


    etc...






    share|improve this answer




























      up vote
      15
      down vote













      In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".



      I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.






      share|improve this answer

















      • 2




        Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
        – Adam
        Feb 9 '17 at 21:03




















      up vote
      6
      down vote













      Using the [key] didn't work for me but using an id property does it.
      I just add this property in my class.



      public int id {get; set}





      share|improve this answer

















      • 1




        It needs to be [Key], but a property called "id" will also work.
        – Michael Blackburn
        May 11 '16 at 15:43




















      up vote
      1
      down vote













      The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key] annotation. Or, the quickest way, add an ID property. Then, the EF framework will take ID as the primary key by default.






      share|improve this answer























      • What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
        – Richard Griffiths
        Mar 3 '16 at 7:33










      • If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
        – Michael Blackburn
        May 11 '16 at 15:44










      • Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
        – Michael Blackburn
        May 11 '16 at 15:45




















      up vote
      1
      down vote













      The object must contain a field which will be used as the Primary Key, if you have a field named Id then this will by default be the primary key of the object to which Entity Framework will link to.



      Otherwise, you should add the [Key] attribute above that field you wanna use as the Primary Key, and you'll also need to add the namespace System.ComponentModel.DataAnnotations:



      public class myClass
      {
      public int Id { get; set; }
      [Key]
      public string Name { get; set; }
      }


      https://stackoverflow.com/a/51180941/7003760






      share|improve this answer




























        up vote
        0
        down vote













        All is right but in my case i have two class like this



        namespace WebAPI.Model
        {
        public class ProductsModel
        {
        [Table("products")]
        public class Products
        {
        [Key]
        public int slno { get; set; }

        public int productId { get; set; }
        public string ProductName { get; set; }

        public int Price { get; set; }
        }
        }
        }


        After deleting the upper class it works fine for me.






        share|improve this answer




























          up vote
          0
          down vote













          For me, the model was fine. It was because I had 2 different versions of entity framework. One version for the web project and another one for the common project which contains the models.



          I just had to consolidate the nuget packages.



          Enjoy!






          share|improve this answer




























            up vote
            0
            down vote













            You don't have to use key attribute all the time. Make sure the mapping file properly addressed the key



            this.HasKey(t => t.Key);
            this.ToTable("PacketHistory");
            this.Property(p => p.Key)
            .HasColumnName("PacketHistorySK");


            and don't forget to add the mapping in the Repository's OnModelCreating



            modelBuilder.Configurations.Add(new PacketHistoryMap());





            share|improve this answer




















              protected by Community May 14 '15 at 0:13



              Thank you for your interest in this question.
              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



              Would you like to answer one of these unanswered questions instead?














              11 Answers
              11






              active

              oldest

              votes








              11 Answers
              11






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              139
              down vote



              accepted










              The Model class should be changed to :



              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.ComponentModel.DataAnnotations.Schema;
              using System.ComponentModel.DataAnnotations;

              namespace MvcApplication1.Models
              {
              [Table("studentdetails")]
              public class student
              {
              [Key]
              public int RollNo { get; set; }

              public string Name { get; set; }

              public string Stream { get; set; }

              public string Div { get; set; }
              }
              }





              share|improve this answer





















              • my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
                – Mihai Bratulescu
                May 29 '14 at 9:31






              • 3




                Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
                – alex
                May 30 '14 at 11:10






              • 1




                And in case you are using it as a model in MVC, don't forget to rebuild!
                – RoJaIt
                Aug 2 at 12:27















              up vote
              139
              down vote



              accepted










              The Model class should be changed to :



              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.ComponentModel.DataAnnotations.Schema;
              using System.ComponentModel.DataAnnotations;

              namespace MvcApplication1.Models
              {
              [Table("studentdetails")]
              public class student
              {
              [Key]
              public int RollNo { get; set; }

              public string Name { get; set; }

              public string Stream { get; set; }

              public string Div { get; set; }
              }
              }





              share|improve this answer





















              • my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
                – Mihai Bratulescu
                May 29 '14 at 9:31






              • 3




                Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
                – alex
                May 30 '14 at 11:10






              • 1




                And in case you are using it as a model in MVC, don't forget to rebuild!
                – RoJaIt
                Aug 2 at 12:27













              up vote
              139
              down vote



              accepted







              up vote
              139
              down vote



              accepted






              The Model class should be changed to :



              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.ComponentModel.DataAnnotations.Schema;
              using System.ComponentModel.DataAnnotations;

              namespace MvcApplication1.Models
              {
              [Table("studentdetails")]
              public class student
              {
              [Key]
              public int RollNo { get; set; }

              public string Name { get; set; }

              public string Stream { get; set; }

              public string Div { get; set; }
              }
              }





              share|improve this answer












              The Model class should be changed to :



              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.ComponentModel.DataAnnotations.Schema;
              using System.ComponentModel.DataAnnotations;

              namespace MvcApplication1.Models
              {
              [Table("studentdetails")]
              public class student
              {
              [Key]
              public int RollNo { get; set; }

              public string Name { get; set; }

              public string Stream { get; set; }

              public string Div { get; set; }
              }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 1 '13 at 16:36









              Coder

              2,37231125




              2,37231125












              • my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
                – Mihai Bratulescu
                May 29 '14 at 9:31






              • 3




                Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
                – alex
                May 30 '14 at 11:10






              • 1




                And in case you are using it as a model in MVC, don't forget to rebuild!
                – RoJaIt
                Aug 2 at 12:27


















              • my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
                – Mihai Bratulescu
                May 29 '14 at 9:31






              • 3




                Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
                – alex
                May 30 '14 at 11:10






              • 1




                And in case you are using it as a model in MVC, don't forget to rebuild!
                – RoJaIt
                Aug 2 at 12:27
















              my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
              – Mihai Bratulescu
              May 29 '14 at 9:31




              my code worked (without [Key]) until I change the data types (I changed them to unsigned) and only byte works is there a reason I can't use unsigned values?
              – Mihai Bratulescu
              May 29 '14 at 9:31




              3




              3




              Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
              – alex
              May 30 '14 at 11:10




              Entity Framework does not support unsigned integers msdn.microsoft.com/en-us/library/vstudio/…
              – alex
              May 30 '14 at 11:10




              1




              1




              And in case you are using it as a model in MVC, don't forget to rebuild!
              – RoJaIt
              Aug 2 at 12:27




              And in case you are using it as a model in MVC, don't forget to rebuild!
              – RoJaIt
              Aug 2 at 12:27












              up vote
              86
              down vote














              1. Make sure public members of student class are defined as properties w/ {get; set;} (yours are public variables - a common mistake).


              2. Place a [Key] annotation on top of your chosen property.







              share|improve this answer



























                up vote
                86
                down vote














                1. Make sure public members of student class are defined as properties w/ {get; set;} (yours are public variables - a common mistake).


                2. Place a [Key] annotation on top of your chosen property.







                share|improve this answer

























                  up vote
                  86
                  down vote










                  up vote
                  86
                  down vote










                  1. Make sure public members of student class are defined as properties w/ {get; set;} (yours are public variables - a common mistake).


                  2. Place a [Key] annotation on top of your chosen property.







                  share|improve this answer















                  1. Make sure public members of student class are defined as properties w/ {get; set;} (yours are public variables - a common mistake).


                  2. Place a [Key] annotation on top of your chosen property.








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 8 '17 at 7:00









                  Cerbrus

                  48.5k989114




                  48.5k989114










                  answered Feb 28 '14 at 12:01









                  Larry Raymond

                  87274




                  87274






















                      up vote
                      67
                      down vote













                      There are several reasons this can happen. Some of these I found here, others I discovered on my own.




                      • If the property is named something other than Id, you need to add the [Key] attribute to it.

                      • The key needs to be a property, not a field.

                      • The key needs to be public

                      • The key needs to be a CLS-compliant type, meaning unsigned types like uint, ulong etc. are not allowed.

                      • This error can also be caused by configuration mistakes.






                      share|improve this answer



















                      • 3




                        I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                        – JohnFx
                        Sep 8 '16 at 21:45






                      • 1




                        @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                        – jeremysawesome
                        Feb 9 '17 at 6:35










                      • In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                        – Angela Amarapala
                        Jul 18 '17 at 7:47















                      up vote
                      67
                      down vote













                      There are several reasons this can happen. Some of these I found here, others I discovered on my own.




                      • If the property is named something other than Id, you need to add the [Key] attribute to it.

                      • The key needs to be a property, not a field.

                      • The key needs to be public

                      • The key needs to be a CLS-compliant type, meaning unsigned types like uint, ulong etc. are not allowed.

                      • This error can also be caused by configuration mistakes.






                      share|improve this answer



















                      • 3




                        I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                        – JohnFx
                        Sep 8 '16 at 21:45






                      • 1




                        @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                        – jeremysawesome
                        Feb 9 '17 at 6:35










                      • In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                        – Angela Amarapala
                        Jul 18 '17 at 7:47













                      up vote
                      67
                      down vote










                      up vote
                      67
                      down vote









                      There are several reasons this can happen. Some of these I found here, others I discovered on my own.




                      • If the property is named something other than Id, you need to add the [Key] attribute to it.

                      • The key needs to be a property, not a field.

                      • The key needs to be public

                      • The key needs to be a CLS-compliant type, meaning unsigned types like uint, ulong etc. are not allowed.

                      • This error can also be caused by configuration mistakes.






                      share|improve this answer














                      There are several reasons this can happen. Some of these I found here, others I discovered on my own.




                      • If the property is named something other than Id, you need to add the [Key] attribute to it.

                      • The key needs to be a property, not a field.

                      • The key needs to be public

                      • The key needs to be a CLS-compliant type, meaning unsigned types like uint, ulong etc. are not allowed.

                      • This error can also be caused by configuration mistakes.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 25 '17 at 9:34









                      Simon

                      20.8k1599159




                      20.8k1599159










                      answered Apr 3 '15 at 6:32









                      BlueRaja - Danny Pflughoeft

                      56.9k21150238




                      56.9k21150238








                      • 3




                        I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                        – JohnFx
                        Sep 8 '16 at 21:45






                      • 1




                        @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                        – jeremysawesome
                        Feb 9 '17 at 6:35










                      • In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                        – Angela Amarapala
                        Jul 18 '17 at 7:47














                      • 3




                        I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                        – JohnFx
                        Sep 8 '16 at 21:45






                      • 1




                        @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                        – jeremysawesome
                        Feb 9 '17 at 6:35










                      • In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                        – Angela Amarapala
                        Jul 18 '17 at 7:47








                      3




                      3




                      I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                      – JohnFx
                      Sep 8 '16 at 21:45




                      I have also found that the key property has to be read-write. I got OP's error when I had a getter and no setter on the ID property.
                      – JohnFx
                      Sep 8 '16 at 21:45




                      1




                      1




                      @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                      – jeremysawesome
                      Feb 9 '17 at 6:35




                      @JohnFx is absolutely correct. Resharper removed private set from some properties during a cleanup. The result was the "EntityType has no key defined error". So beware of cleanup tools removing necessary code.
                      – jeremysawesome
                      Feb 9 '17 at 6:35












                      In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                      – Angela Amarapala
                      Jul 18 '17 at 7:47




                      In my case, I used some unrelated name to mention the Id of the class. Changing that as mentioned in your solution solved my problem. Thankyou :)
                      – Angela Amarapala
                      Jul 18 '17 at 7:47










                      up vote
                      19
                      down vote













                      i know that this post is late but
                      this solution helped me:



                          [Key]
                      [Column(Order = 0)]
                      public int RoleId { get; set; }


                      added [Column(Order = 0)]
                      after [Key]
                      can be added by increment by 1:



                          [Key]
                      [Column(Order = 1)]
                      public int RoleIdName { get; set; }


                      etc...






                      share|improve this answer

























                        up vote
                        19
                        down vote













                        i know that this post is late but
                        this solution helped me:



                            [Key]
                        [Column(Order = 0)]
                        public int RoleId { get; set; }


                        added [Column(Order = 0)]
                        after [Key]
                        can be added by increment by 1:



                            [Key]
                        [Column(Order = 1)]
                        public int RoleIdName { get; set; }


                        etc...






                        share|improve this answer























                          up vote
                          19
                          down vote










                          up vote
                          19
                          down vote









                          i know that this post is late but
                          this solution helped me:



                              [Key]
                          [Column(Order = 0)]
                          public int RoleId { get; set; }


                          added [Column(Order = 0)]
                          after [Key]
                          can be added by increment by 1:



                              [Key]
                          [Column(Order = 1)]
                          public int RoleIdName { get; set; }


                          etc...






                          share|improve this answer












                          i know that this post is late but
                          this solution helped me:



                              [Key]
                          [Column(Order = 0)]
                          public int RoleId { get; set; }


                          added [Column(Order = 0)]
                          after [Key]
                          can be added by increment by 1:



                              [Key]
                          [Column(Order = 1)]
                          public int RoleIdName { get; set; }


                          etc...







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 31 '14 at 20:22









                          danigitman

                          45549




                          45549






















                              up vote
                              15
                              down vote













                              In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".



                              I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.






                              share|improve this answer

















                              • 2




                                Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                                – Adam
                                Feb 9 '17 at 21:03

















                              up vote
                              15
                              down vote













                              In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".



                              I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.






                              share|improve this answer

















                              • 2




                                Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                                – Adam
                                Feb 9 '17 at 21:03















                              up vote
                              15
                              down vote










                              up vote
                              15
                              down vote









                              In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".



                              I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.






                              share|improve this answer












                              In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".



                              I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 18 '14 at 23:21









                              ChrisS

                              37934




                              37934








                              • 2




                                Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                                – Adam
                                Feb 9 '17 at 21:03
















                              • 2




                                Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                                – Adam
                                Feb 9 '17 at 21:03










                              2




                              2




                              Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                              – Adam
                              Feb 9 '17 at 21:03






                              Yes - 'Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.' taken from this page
                              – Adam
                              Feb 9 '17 at 21:03












                              up vote
                              6
                              down vote













                              Using the [key] didn't work for me but using an id property does it.
                              I just add this property in my class.



                              public int id {get; set}





                              share|improve this answer

















                              • 1




                                It needs to be [Key], but a property called "id" will also work.
                                – Michael Blackburn
                                May 11 '16 at 15:43

















                              up vote
                              6
                              down vote













                              Using the [key] didn't work for me but using an id property does it.
                              I just add this property in my class.



                              public int id {get; set}





                              share|improve this answer

















                              • 1




                                It needs to be [Key], but a property called "id" will also work.
                                – Michael Blackburn
                                May 11 '16 at 15:43















                              up vote
                              6
                              down vote










                              up vote
                              6
                              down vote









                              Using the [key] didn't work for me but using an id property does it.
                              I just add this property in my class.



                              public int id {get; set}





                              share|improve this answer












                              Using the [key] didn't work for me but using an id property does it.
                              I just add this property in my class.



                              public int id {get; set}






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 26 '15 at 21:24









                              William Bello

                              127211




                              127211








                              • 1




                                It needs to be [Key], but a property called "id" will also work.
                                – Michael Blackburn
                                May 11 '16 at 15:43
















                              • 1




                                It needs to be [Key], but a property called "id" will also work.
                                – Michael Blackburn
                                May 11 '16 at 15:43










                              1




                              1




                              It needs to be [Key], but a property called "id" will also work.
                              – Michael Blackburn
                              May 11 '16 at 15:43






                              It needs to be [Key], but a property called "id" will also work.
                              – Michael Blackburn
                              May 11 '16 at 15:43












                              up vote
                              1
                              down vote













                              The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key] annotation. Or, the quickest way, add an ID property. Then, the EF framework will take ID as the primary key by default.






                              share|improve this answer























                              • What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                                – Richard Griffiths
                                Mar 3 '16 at 7:33










                              • If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                                – Michael Blackburn
                                May 11 '16 at 15:44










                              • Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                                – Michael Blackburn
                                May 11 '16 at 15:45

















                              up vote
                              1
                              down vote













                              The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key] annotation. Or, the quickest way, add an ID property. Then, the EF framework will take ID as the primary key by default.






                              share|improve this answer























                              • What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                                – Richard Griffiths
                                Mar 3 '16 at 7:33










                              • If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                                – Michael Blackburn
                                May 11 '16 at 15:44










                              • Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                                – Michael Blackburn
                                May 11 '16 at 15:45















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key] annotation. Or, the quickest way, add an ID property. Then, the EF framework will take ID as the primary key by default.






                              share|improve this answer














                              The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key] annotation. Or, the quickest way, add an ID property. Then, the EF framework will take ID as the primary key by default.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 15 '15 at 12:53









                              Paul Stenne

                              5,84063048




                              5,84063048










                              answered Mar 15 '15 at 12:01









                              UtillYou

                              111




                              111












                              • What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                                – Richard Griffiths
                                Mar 3 '16 at 7:33










                              • If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                                – Michael Blackburn
                                May 11 '16 at 15:44










                              • Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                                – Michael Blackburn
                                May 11 '16 at 15:45




















                              • What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                                – Richard Griffiths
                                Mar 3 '16 at 7:33










                              • If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                                – Michael Blackburn
                                May 11 '16 at 15:44










                              • Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                                – Michael Blackburn
                                May 11 '16 at 15:45


















                              What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                              – Richard Griffiths
                              Mar 3 '16 at 7:33




                              What happens if the database has more than one field suffixed with ID? On regeneration I lose the Key attribute.
                              – Richard Griffiths
                              Mar 3 '16 at 7:33












                              If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                              – Michael Blackburn
                              May 11 '16 at 15:44




                              If you're generating the code from the DB, one of the columns in the table should be the table's primary Key.
                              – Michael Blackburn
                              May 11 '16 at 15:44












                              Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                              – Michael Blackburn
                              May 11 '16 at 15:45






                              Regarding "editing" generated code, it's almost always generated as a partial class. You can create a second file as a partial class with the same name, and add the property again with the [Key] attribute.
                              – Michael Blackburn
                              May 11 '16 at 15:45












                              up vote
                              1
                              down vote













                              The object must contain a field which will be used as the Primary Key, if you have a field named Id then this will by default be the primary key of the object to which Entity Framework will link to.



                              Otherwise, you should add the [Key] attribute above that field you wanna use as the Primary Key, and you'll also need to add the namespace System.ComponentModel.DataAnnotations:



                              public class myClass
                              {
                              public int Id { get; set; }
                              [Key]
                              public string Name { get; set; }
                              }


                              https://stackoverflow.com/a/51180941/7003760






                              share|improve this answer

























                                up vote
                                1
                                down vote













                                The object must contain a field which will be used as the Primary Key, if you have a field named Id then this will by default be the primary key of the object to which Entity Framework will link to.



                                Otherwise, you should add the [Key] attribute above that field you wanna use as the Primary Key, and you'll also need to add the namespace System.ComponentModel.DataAnnotations:



                                public class myClass
                                {
                                public int Id { get; set; }
                                [Key]
                                public string Name { get; set; }
                                }


                                https://stackoverflow.com/a/51180941/7003760






                                share|improve this answer























                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote









                                  The object must contain a field which will be used as the Primary Key, if you have a field named Id then this will by default be the primary key of the object to which Entity Framework will link to.



                                  Otherwise, you should add the [Key] attribute above that field you wanna use as the Primary Key, and you'll also need to add the namespace System.ComponentModel.DataAnnotations:



                                  public class myClass
                                  {
                                  public int Id { get; set; }
                                  [Key]
                                  public string Name { get; set; }
                                  }


                                  https://stackoverflow.com/a/51180941/7003760






                                  share|improve this answer












                                  The object must contain a field which will be used as the Primary Key, if you have a field named Id then this will by default be the primary key of the object to which Entity Framework will link to.



                                  Otherwise, you should add the [Key] attribute above that field you wanna use as the Primary Key, and you'll also need to add the namespace System.ComponentModel.DataAnnotations:



                                  public class myClass
                                  {
                                  public int Id { get; set; }
                                  [Key]
                                  public string Name { get; set; }
                                  }


                                  https://stackoverflow.com/a/51180941/7003760







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jul 5 at 20:38









                                  Mayer Sariggs

                                  482313




                                  482313






















                                      up vote
                                      0
                                      down vote













                                      All is right but in my case i have two class like this



                                      namespace WebAPI.Model
                                      {
                                      public class ProductsModel
                                      {
                                      [Table("products")]
                                      public class Products
                                      {
                                      [Key]
                                      public int slno { get; set; }

                                      public int productId { get; set; }
                                      public string ProductName { get; set; }

                                      public int Price { get; set; }
                                      }
                                      }
                                      }


                                      After deleting the upper class it works fine for me.






                                      share|improve this answer

























                                        up vote
                                        0
                                        down vote













                                        All is right but in my case i have two class like this



                                        namespace WebAPI.Model
                                        {
                                        public class ProductsModel
                                        {
                                        [Table("products")]
                                        public class Products
                                        {
                                        [Key]
                                        public int slno { get; set; }

                                        public int productId { get; set; }
                                        public string ProductName { get; set; }

                                        public int Price { get; set; }
                                        }
                                        }
                                        }


                                        After deleting the upper class it works fine for me.






                                        share|improve this answer























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          All is right but in my case i have two class like this



                                          namespace WebAPI.Model
                                          {
                                          public class ProductsModel
                                          {
                                          [Table("products")]
                                          public class Products
                                          {
                                          [Key]
                                          public int slno { get; set; }

                                          public int productId { get; set; }
                                          public string ProductName { get; set; }

                                          public int Price { get; set; }
                                          }
                                          }
                                          }


                                          After deleting the upper class it works fine for me.






                                          share|improve this answer












                                          All is right but in my case i have two class like this



                                          namespace WebAPI.Model
                                          {
                                          public class ProductsModel
                                          {
                                          [Table("products")]
                                          public class Products
                                          {
                                          [Key]
                                          public int slno { get; set; }

                                          public int productId { get; set; }
                                          public string ProductName { get; set; }

                                          public int Price { get; set; }
                                          }
                                          }
                                          }


                                          After deleting the upper class it works fine for me.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jun 13 '17 at 15:52









                                          Debendra Dash

                                          2,0371921




                                          2,0371921






















                                              up vote
                                              0
                                              down vote













                                              For me, the model was fine. It was because I had 2 different versions of entity framework. One version for the web project and another one for the common project which contains the models.



                                              I just had to consolidate the nuget packages.



                                              Enjoy!






                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote













                                                For me, the model was fine. It was because I had 2 different versions of entity framework. One version for the web project and another one for the common project which contains the models.



                                                I just had to consolidate the nuget packages.



                                                Enjoy!






                                                share|improve this answer























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  For me, the model was fine. It was because I had 2 different versions of entity framework. One version for the web project and another one for the common project which contains the models.



                                                  I just had to consolidate the nuget packages.



                                                  Enjoy!






                                                  share|improve this answer












                                                  For me, the model was fine. It was because I had 2 different versions of entity framework. One version for the web project and another one for the common project which contains the models.



                                                  I just had to consolidate the nuget packages.



                                                  Enjoy!







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jun 17 at 15:34









                                                  Daniel

                                                  5,75312837




                                                  5,75312837






















                                                      up vote
                                                      0
                                                      down vote













                                                      You don't have to use key attribute all the time. Make sure the mapping file properly addressed the key



                                                      this.HasKey(t => t.Key);
                                                      this.ToTable("PacketHistory");
                                                      this.Property(p => p.Key)
                                                      .HasColumnName("PacketHistorySK");


                                                      and don't forget to add the mapping in the Repository's OnModelCreating



                                                      modelBuilder.Configurations.Add(new PacketHistoryMap());





                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        You don't have to use key attribute all the time. Make sure the mapping file properly addressed the key



                                                        this.HasKey(t => t.Key);
                                                        this.ToTable("PacketHistory");
                                                        this.Property(p => p.Key)
                                                        .HasColumnName("PacketHistorySK");


                                                        and don't forget to add the mapping in the Repository's OnModelCreating



                                                        modelBuilder.Configurations.Add(new PacketHistoryMap());





                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          You don't have to use key attribute all the time. Make sure the mapping file properly addressed the key



                                                          this.HasKey(t => t.Key);
                                                          this.ToTable("PacketHistory");
                                                          this.Property(p => p.Key)
                                                          .HasColumnName("PacketHistorySK");


                                                          and don't forget to add the mapping in the Repository's OnModelCreating



                                                          modelBuilder.Configurations.Add(new PacketHistoryMap());





                                                          share|improve this answer












                                                          You don't have to use key attribute all the time. Make sure the mapping file properly addressed the key



                                                          this.HasKey(t => t.Key);
                                                          this.ToTable("PacketHistory");
                                                          this.Property(p => p.Key)
                                                          .HasColumnName("PacketHistorySK");


                                                          and don't forget to add the mapping in the Repository's OnModelCreating



                                                          modelBuilder.Configurations.Add(new PacketHistoryMap());






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Oct 4 at 16:39









                                                          Tosh

                                                          34353




                                                          34353

















                                                              protected by Community May 14 '15 at 0:13



                                                              Thank you for your interest in this question.
                                                              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                              Would you like to answer one of these unanswered questions instead?



                                                              Popular posts from this blog

                                                              Guess what letter conforming each word

                                                              Port of Spain

                                                              Run scheduled task as local user group (not BUILTIN)