How to set id key to be generated by the database in Entity Framework after model creation using Database First

entity-framework

Question

I am using Entity Framework in a Web API project. I have created my classes and models from an existing Database (MySQL), so I basically used the EF DbContextGenerator to generate my classes from my EDMX model.

Read operations are working fine, but I am now at the point where I want to start adding functionality to add records to the database. I want the id for entities to be automatically assigned by the database.

It seems like when you are following a code-first approach, one simply needs to specify:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

In the class definition for the Id property. So I am tempted to simply add the "DatabaseGeneratedOption" to the class file that was generated. However, since I am using a database-first approach and my classes are basically auto-generated for me, I am not supposed to edit the class files as they will get overwritten again should I re-generate the classes again. Where/How do I set the Id value to be generated by the database rather than by the EF code?

1
3
9/30/2013 9:09:51 AM

Accepted Answer

It's an option in the properties of a property (o_O) in the edmx designer:

enter image description here

6
9/30/2013 10:04:35 AM


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