Entity Framework 4.3 with Code First

So today was my first day trying out Entity Framework’s Code First mechanism. I have to say, I actually like it. Some notes for other people getting started with it:

  • The easiest way to get started is to use “convention over configuration”
  • For entity classes, they should be a POCO. For your ID field, it should be the name of the class with “ID” appended. If the entity class is “TestEntity“, your ID field should be “TestEntityID“.
  • When creating the DbSet<T> getting in your DbContext subclass, name the getter the same as the database table name. For instance, if your database table is named TestEntities, and it corresponds to the entity TestEntity, your getter should be something like¬†public DbSet<TestEntity> TestEntities { get; set; }
  • Name your DbContext subclass the same as your connectionString in your App.config / Web.config file. For instance, if your DbContext subclass is TestDataContext, name your connection string TestDataContext as well, for instance like:¬†<add name=”TestDataContext” connectionString=”Data Source=.;Database=TestEntityFrameworkDatabase;Integrated Security=true;” providerName=”System.Data.SqlClient”/>

Makes it nice and easy. No other configuration needed in code or any config files. No database metadata files, etc…

Leave a Reply

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