Code Generation : Add primary key / foreign key to Entity Framework

c# code-generation entity-framework-6

Question

I'm using EF with database first approach.

I would like to add attribute to the generated code to mark property as primary key / foreign key when it's the case.

For example, if I have a table User in my database

CREATE TABLE User
(
   Id int PRIMARY KEY
);

I would like my generated code to include an attribute that indicate that Id is the primary key.

public partial class User
{
    [PrimaryKey]
    public int Id { get; set; }
}

How could I do that?

EDIT : I know about T4 template, I just don't know where to start to modify that file.

1
0
11/5/2014 4:22:53 PM

Popular Answer

I ran into the same problem. The template file you are looking for appears in the Solution Explorer (in VS2013) as:

YourDataModelName.tt

You can modify it to add the [Key] attributes to the generated code by following:

Finding a property is Primary Key in POCO Template t4 generator

1
5/23/2017 12:28:28 PM


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