.NET Tips and Tricks

Blog archive

Defining Natural Keys in Entity Framework Entity Classes

It's not something database developers do much anymore, but it is possible to have a primary key in a table that isn't generated by the database. If you do have a key like that, here's what you have to do: when writing an entity class in Entity Framework, you need to tell Entity Framework about it by flagging the corresponding property in the entity class. You do that by decorating the property with the DatabaseGenerated attribute and specifying a DatabaseGeneratedOption.

For a key not generated by the database, the option to pick is DatabaseGeneratedOption.None, like this:

 Public  Class SalesOrder
<Key(), DatabaseGenerated(DatabaseGeneratedOption.None)> Public Property SalesOrderId As Integer

Posted by Peter Vogel on 05/19/2014 at 12:32 PM


comments powered by Disqus
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.