Using code first with Entity Framework for generating database

I was working on a personal project today, and did a lot of experiments on Code First Model with Entity Framework. All of these were quite amazing and revealed a lot of underlying implementation and how things have progressed in Entity Framework world. I looked at various articles which helped me understand all these aspects, and am grateful of all of them. So I thought why not write some posts of mine too, just to document what I have learned, so that others might benefit from it too.

For that purpose I am starting with a simplest item, I will try to generate a database for a single class, and see how it looks. By the way I am using Entity Framework 5.

Here is the class, which I will work on.

However to generate database for this class, I have to create a class which inherits from DBContext.

Then I created a test class which will initiate the database generation, note that I have not assigned all the properties.

When I executed this, I was struck with following exception, I should have set birthdate too.

schema generation exception

Why not have a look on the generated database.

generated table schema

There it is, the birthdate column was marked not null, that’s why we got that exception earlier. that was easily fixed by making birthdate as Nullable<>.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.