Entity framework code first example

Workign with DbContext

DbContext is a leightweight version of the ObjectContext wich was used prior entity framework 4.1. The DbContext is your primairy class for dealing with data. When working with entity framework in the designer interface the DbContext class is autogenerated. Working with entity framework code first you will have to type it yourself.

    public class Database : DbContext
    {
        // constructor
        public Database() : base("name=Data") { }

        // propertys
        public virtual DbSet<Customer> Customers { get; set; }
    }

It's that simple to create a class wich let's you interact with the database. The entity will connect to the database according to your connections string. In this case it wil search in the web.config for the connectionstring with the name Data. A entity connection string is generally looking like this:

  <connectionStrings>
    <add name="connectionName" connectionString="data source=.sqlexpress;initial catalog=database;persist security info=True;user id=sa;Password=sa_pwd;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>

After finding your connectionstring it will connect to the database and investigate your class for types and will create a table for all types discovered in your database class. In this case there is a type customers, so it will create a table according your class customer:

    public class Customer:Person
    {
        public int PersonId { get; set; }
        public int CustomerNumber { get; set; }
        public int KVKnumber { get; set; }

        public virtual ICollection<Address> Adresses { get; set; }
    }

While investigation the customer class the entity framework discovers more types which and will try to create a table for those types as well. F.i. he will find the Addess type and will go to the Addess type and parse it to a table. He allso find a Person type and discovers that it's used in an inheritance situation and depending on the choosen inheritance strategy it will create a table or add the property's of the inherited class to the table. Here yo will find more information on entity framework and the inheritance strategy.

Pluralization

Standard the entity framework will pluralize your classess to pluralized table names. F.i. you model class Customer will become the table: Customers. This is completely right but in some cases we don't want that behaviour and you have to override the onModelCreating method and remove the conventions.

    public class Database : DbContext
    {
        // constructor
        public Database() : base("name=Data") { }

        // propertys
        public virtual ICollection Customers { get; set; }

        // methods
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!