Following the sample EF CTP4 Walkthrough: Productivity Improvements you end up with a sample DbContext and of course I immediately thought of using this in Dynamic Data using the “ASP.NET Dynamic Data Entities Web Application” template (Figure 1),
Figure 1 - Select ASP.NET Dynamic Data Entities Web Application
but I got the following error:
Figure 2 – Error when using DbContext with DD4 EF Template.
This my my default code Listing 1
DefaultModel.RegisterContext(typeof(Models.ProductCatalog), new ContextConfiguration() { ScaffoldAllTables = true });
Listing 1 – my default DbContext registration
So I fired off an e-mail to Microsoft and was told “the guideline will be to use DD with DomainService, which will have full support of DbContext in the future” although that wont work out of the box at the moment so, I gave up
Then Diego Vega posted on twitter a link to Rowan Millers blog EF CTP4 Tips & Tricks: WCF Data Service on DbContext this turned out to be very useful, he talks about getting the underlying ObjectContext see Listing 2.
public class ProductCatalog : DbContext { public ProductCatalog(String connectionString) { // set default connection string this.Database.Connection.ConnectionString = connectionString; } public ObjectContext UnderlyingContext { get { return this.ObjectContext; } } public DbSet<Category> Categories { get; set; } public DbSet<Product> Products { get; set; } } public class Category { public string CategoryId { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } public class Product { public int ProductId { get; set; } public string Name { get; set; } public string CategoryId { get; set; } public virtual Category Category { get; set; } }
Listing 2 – my ProductCatalog
In Listing 2 I use this feature to get the underlying ObjectContext (Also note my setting the connection string in the constructor). So now we have a cool way of getting Entity Framework Code First CTP4 working with Dynamic Data 4. This makes me very happy
So you will see that all we need to do to get this working with Dynamic Data now is to replace the
typeof(Models.ProductCatalog)
with
() => new Models.ProductCatalog(connectionString).UnderlyingContext
in the RegisterContext method to get at the underlying ObjectContext, which now leads us to Listing 3 .
public static void RegisterRoutes(RouteCollection routes) { var connectionString = "Data Source=aragorn;Initial Catalog=ProductCatalog;Integrated Security=True"; DefaultModel.RegisterContext(() => new Models.ProductCatalog(connectionString).UnderlyingContext, new ContextConfiguration() { ScaffoldAllTables = true }); routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = DefaultModel }); }
Listing 3 – Global.asax RegisterRoutes method.
Now we can Edit, Update and Insert using Entity Framework Code First feature in CTP4.
5 comments:
Hi,
Thank you for your post it's just what I was waiting for before trying EF code first :)
Did you find a way to exclude the EdmMetadataSet table from the scaffodling?
Guillaume
Hi Guillaume, not sure about that, but I'll have a look.
I've just excluded it from the index page with a bit of Linq:
VisibleTables.Where(t => t.Name != "EdmMetadataSet") ;)
Ah Guillaume, got it, that is one of your tables yes :)
Steve
For EF5 see Using Dynamic Data with Entity Framework DbContext
Steve
Post a Comment