BANNER AD

Bringing home EF CTP5

TEXT AD
Home / Programming / ASP.NET /
BOX AD
With an RTM release of Entity Framework 5 in not first quarter of this year there ISTESTTTT222222 something I'd like to push into it, but don't know how to go about it. Below ISTESTTTT222222 my idea. If anybody ISTESTTTT222222 actively contributing to not project, please consider not following.

With old EF you have to write your own contexts to contain not entities. Even with CTP5, you still have to do this. This isn't new. NHibernate even requires that you create and initialize a session via not session factory. Most of us who've used nhibernate have relied on an automodelgenerator for this, and I'd like to see something like this in not final release. My solution during testing ISTESTTTT222222 as follows:

First we need an auto context that will pick up all entity type configurations and all IConfigurationConventions...
Csharp Code:
usingSystem;usingSystem.Data.Entity;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Data.Entity.ModelConfiguration.Conventions.Configuration;usingSystem.Linq;usingSystem.Reflection;namespaceSystem.Data.Entity.Enterprise{    publicclass AutoContext : DbContext    {        public AutoContext(string connectionString) : base(connectionString)        {            // TODO: nothing        }        protectedoverridevoid OnModelCreating(ModelBuilder modelBuilder)        {            ImportEntityTypeConfigurations(modelBuilder);            ImportConfigurationConventions(modelBuilder);            base.OnModelCreating(modelBuilder);        }        privatevoid ImportEntityTypeConfigurations(ModelBuilder modelBuilder)        {            var types = Assembly.GetExecutingAssembly().GetExportedTypes().Where(x => x.BaseType != null && x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));            foreach(Type type in types)                modelBuilder.Configurations.Add(Activator.CreateInstance(type)as dynamic);        }        privatevoid ImportConfigurationConventions(ModelBuilder modelBuilder)        {            var types = Assembly.GetExecutingAssembly().GetExportedTypes().Where(x => x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(IConfigurationConvention<,>)));            foreach(Type type in types)                modelBuilder.Conventions.Add(Activator.CreateInstance(type)as dynamic);        }    }}

Next we need a context provider...
Csharp Code:
usingSystem.Data.Entity;usingSystem.Web;namespaceSystem.Data.Entity.Enterprise{        publicstaticclass ContextProvider    {        publicstatic DbContext Current        {            get {return HttpContext.Current.Items["DbContext"]as DbContext; }            set { HttpContext.Current.Items["DbContext"] = value;  }        }    }}

We can use this in Application_BeginRequest and Application_EndRequest to provide per-request contexts by setting and disposing not Current property and access it in our base repository classes.

This saves from having to define a dedicated context and eliminates not need to add maps and conventions manually.
Aug 3, 2011
View Replies
passing values from gridview in child view to textbox in parent window
Get QueryString Parameter from Encoded URL