appenthused
appenthused

Reputation: 183

how to generate a database table from an entity type?

How to create an entity type and then generate a database table from it? I know this feature was not supported two years ago in EF, what about now?

Upvotes: 0

Views: 65

Answers (2)

Elias Hossain
Elias Hossain

Reputation: 4469

Now, Entity Framework introduced these feature. Basically, with only two steps is sufficient for this, please see below steps to go:

  1. Create your Entity

    public class Resturant
    {
       public int ID { get; set; }
       public string Name { get; set; }
    }
    
  2. Create Context class

    public class OdeToFoodDb: DbContext
    {
       public DbSet<Resturant> Resturants { get; set; }
    }
    

However, you may need more coding in Global.ascx for advance options but these are the basic steps.

A database named "OdeToFoodDb" will create and a table named "Resturant" also will create by these steps.

Upvotes: 0

TBohnen.jnr
TBohnen.jnr

Reputation: 5129

You've got 2 options:

Entity Framework Model First where you create the model first and then generate the database from that or Entity Framework Code First where you create normal Poco objects and generate the database from that.

I've personally used Entity Framework Code First for MVC development and it works like a charm, it really is an awesome feature and easy to use.

Upvotes: 2

Related Questions