Assigning [Key] attribute to Entity Framework generated class - MVC4 C#

Matt Koch

I am working on an MVC 4 (C#) project in Visual Studio 2012. I am using the Entity Framework which utilizes an *.edmx file to model an existing database. My goal is to implement database auditing to track any changes being made to the database. Classes are generated automatically with the fields from the database tables - pretty standard stuff, I believe.

I am running into a problem when attempting to retrieve the primary key of these generated classes. The Entity Framework does not assign the [Key] attribute to the class properties. I attempted to solve this by extending the partial classes that were created and assigning this attribute myself. Adding it directly to the generated class works fine. If I try the "buddy class" method, I get the following error later on in my code execution: "Entity {Class Name} has no [Key] attribute."

Auto-Generated class:

public partial class List
{
   public List()
   {
       this.Tasks = new HashSet<Task>();
   }

   public int Id { get; set; }
   public string ListName { get; set; }
}

Extension (buddy) class:

[MetadataType(typeof(ListMetaData))]
public partial class List
{

    private class ListMetaData 
    {
        [Key]
        public int Id { get; set; }

    }

}

Code snippet:

var property = dbEntry.Entity.GetType().GetProperties().FirstOrDefault(
   p => p.GetCustomAttributes(typeof(KeyAttribute), true).Any());

if (property == null)
    throw new InvalidOperationException(string.Format(
    "Entity {0} has no [Key] attribute.", dbEntry.Entity.GetType().Name));

string keyName = property.Name; 

I am overriding the SaveChanges() method in DbContext to track the changes being made. The snippet above shows a portion of code from a function that examines a DbEntityEntry object (dbEntry) to get the primary key.

Matt Koch

Thanks for the suggestions everyone. What I ended up doing is modifying the *.tt template to automatically add the [Key] Attribute and the System.ComponentModel.DataAnnotations Directive to dynamically generated class files.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assigning [Key] attribute to Entity Framework generated class - MVC4 C#

From Dev

ASP.NET MVC 6 - Assigning NOT NULL to the Primary Key in Entity Framework 7

From Dev

Extend property auto generated class by entity framework

From Dev

Data Annotations to Entity Framework generated class

From Dev

Extend property auto generated class by entity framework

From Dev

How to extend an Entity Framework 6.1.3 generated class?

From Dev

PieChart in mvc4 from Entity Framework

From Dev

Dropdownlist and MVC4 Entity Framework

From Dev

C# Entity Framework 6 foreign key constraint in an unexpected format being generated

From Dev

Non-Entity Class Requiring [Key] Attribute

From Dev

ASP.NET MVC How to access Entity Framework generated foreign key?

From Dev

ScriptIgnore attribute in Entity Framework 6 breaks foreign key attribute

From Dev

Entity framework sharing foreign key in base class

From Dev

Log4net with Entity Framework 5 and MVC4

From Dev

structure of the classes having foreign key relationship generated from entity framework

From Dev

Can you access Scaffold attribute property of a class in C# and Entity Framework?

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

Will Orchard CMS support MVC4 with Entity Framework

From Dev

MVC4 Entity Framework Controller Database Issue

From Dev

MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

MVC4 Entity Framework 6 Model State Validation Issues

From Dev

Entity Framework Code First from database not adding Key attribute

From Dev

Set Class Attribute for certain records of Dropdownlist in MVC4

From Dev

Foreign Key in Entity Framework in C#

From Dev

MVC 4 Entity Framework Optional Foreign Key On User Profile

From Dev

Entity framework, can I map a class to a key/value table?

From Dev

Entity framework, can I map a class to a key/value table?

From Dev

Entity Framework generated SQL queries

Related Related

  1. 1

    Assigning [Key] attribute to Entity Framework generated class - MVC4 C#

  2. 2

    ASP.NET MVC 6 - Assigning NOT NULL to the Primary Key in Entity Framework 7

  3. 3

    Extend property auto generated class by entity framework

  4. 4

    Data Annotations to Entity Framework generated class

  5. 5

    Extend property auto generated class by entity framework

  6. 6

    How to extend an Entity Framework 6.1.3 generated class?

  7. 7

    PieChart in mvc4 from Entity Framework

  8. 8

    Dropdownlist and MVC4 Entity Framework

  9. 9

    C# Entity Framework 6 foreign key constraint in an unexpected format being generated

  10. 10

    Non-Entity Class Requiring [Key] Attribute

  11. 11

    ASP.NET MVC How to access Entity Framework generated foreign key?

  12. 12

    ScriptIgnore attribute in Entity Framework 6 breaks foreign key attribute

  13. 13

    Entity framework sharing foreign key in base class

  14. 14

    Log4net with Entity Framework 5 and MVC4

  15. 15

    structure of the classes having foreign key relationship generated from entity framework

  16. 16

    Can you access Scaffold attribute property of a class in C# and Entity Framework?

  17. 17

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  18. 18

    Will Orchard CMS support MVC4 with Entity Framework

  19. 19

    MVC4 Entity Framework Controller Database Issue

  20. 20

    MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

  21. 21

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  22. 22

    MVC4 Entity Framework 6 Model State Validation Issues

  23. 23

    Entity Framework Code First from database not adding Key attribute

  24. 24

    Set Class Attribute for certain records of Dropdownlist in MVC4

  25. 25

    Foreign Key in Entity Framework in C#

  26. 26

    MVC 4 Entity Framework Optional Foreign Key On User Profile

  27. 27

    Entity framework, can I map a class to a key/value table?

  28. 28

    Entity framework, can I map a class to a key/value table?

  29. 29

    Entity Framework generated SQL queries

HotTag

Archive