Design Meeting Notes – January 9, 2014

Scaffolded empty DbContext for Code First

The EF tooling is currently being updated so that the wizard for “ADO.NET Entity Data Model” supports creating Code First models. This will support reverse engineering from an existing database but also scaffolding a new empty DbContext to get people started writing Code First for a new database. This is the proposal for what the scaffolded class should look like:

namespace TestCSharpCodeGen
{
    using System;
    using System.Data.Entity;
    using System.Linq;

    public class MyModel : DbContext
    {
        // Comment indicating that a created connection string is referenced here
        public MyModel()
            : base("name=MyModel")
        {
        }

        // Comment with fwlink indicating what to do next
        // public DbSet<MyEntity> Entities { get; set; }
    }

    public class MyEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

 

Points considered:

  • Which namespaces should be included? Only System.Data.Entity is needed. Some people like to have System and System.Linq as well; others don’t want anything that isn’t needed. Given the target audience we decided that having the System types and the LINQ extension methods available may help people so we will include them.
  • The wizard will create a connection string for LocalDb and put it in the app/web config. The DbContext constructor is setup to use this connection string with the “name=” syntax so that it will throw if the connection string cannot be found.
  • We decided not to add XML doc comments because they don’t really add value for this simple scaffolded code. This will violate some FxCop rules if compiled as-is, but since this is almost entirely example code that the app developer will need to modify anyway it doesn’t seem like this will be a big problem.
  • We will have some other comments and a fwlink to help people figure out what to do next.
  • We add an example entity and an example DbSet. The DbSet is commented out so that if the database is created by running the app then it will be an empty database.
  • The names “MyModel” and "TestCSharpCodeGen" in the example are entered by the app developer in the wizard.

Last edited Jan 9, 2014 at 6:28 PM by ajcvickers, version 3