Upgrade to EF 4.1 (Code First)

Apr 10, 2011 at 5:22 PM

Is there any plan to upgrade the Kigg data access layer (DAL)?  If not, I would appreciate at least some guidance on how to migrate the cuurent DAL to the EF 4.1.  Thanks.

Sep 9, 2011 at 6:37 PM

Since nobidy responed, here is my contribution for people who want to upgrade the Kigg data layer to the latest EF 4.1.

Simplest approach will be to get the ObjectContect from the EF 4,1 Code First DbContext.  Here is How:

public class MyDataContext: DbContext
                   public DbSet<Product> Products { get; set; }
                   //other dbsets, ctor etc.

                  public ObjectContext ObjectContext()
                        return (this as IObjectContextAdapter).ObjectContext;

// You can use the follwing in the Database class

MyDataContext dc = new MyDataContext();

public IQueryable<Product> ProductDataSource
           var context = dc.ObjectContext();

           return context.CreateObjectSet<Product>().Where(p=>p.Discontinued == false).AsQueryable<Product>();

(For more generic query, you can use DbContext.Database.SqlQuery

and DbContext.Database.ExecuteSqlCommand.)

Once you have the ObjectContect, you may also create a generic entity query:

ObjectQuery<TEntity> ObjectContext.CreateQuery<TEntity>(string entitySet);

if we had EDM, a typed context would be generated and have property getters of the form:

ObjectQuery<TEntity> MyEntity which query the default entity set for the entity.

You can then apply LinqToEntities operators to the returned ObjectQuery.


I hope that this is helpful for someone.

Sep 9, 2011 at 8:14 PM

Here is another suggestion on simplifying the kigg data layer using the EF 4.1 Code First.

The EF 4.1 Code First provides three APIs that allow ad hoc sql queries: They are:

1. public int ExecuteSqlCommand(string sql, params object[] parameters); We can use this method to send SQL command (either a stored procedure call or ad-hoc SQL) directly to the database. It delivers a little more than ADO.NET in that it’s able to convert the data-readers directly in entities.

2. public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters); TElement could be any class. An important characteristic is that EF doesn’t track returned object, even if they are entity types. This contrasts with the third API, part of DbSet, which tracks returned objects.

3. DbSet.SqlQuery

However, they miss a lot of benefits of using EF and restrictive. (e.g., you still cannot override the column mapping).

Instead of direct SQL queries, I recommend using a more efficient query to clean up the Kigg Data layer:

First, you could create an entity set without exposing it:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<SimplifiedEntry>().ToTable("SomeEntry", "man");
.Property(s => s.ID)
.Property(s => s.Name)


Then you expose a query:

public IEnumerable<SimplifiedEntry> GetSimpleEntries()
IObjectContextAdapter adapter = this;
var entries = adapter.ObjectContext.CreateQuery<SimplifiedEntry>("SELECT VALUE SomeEntry FROM SomeEntry");

return entries;

In this apprach, you can query the resulting query further and the query will be combined before being sent to SQL . Therefore you have a query returning everything (all features of ObjectContext) and you can further refine the query with LINQ afterwards without materializing the entire table on the client side.

I hope it is useful for someone.

Sep 13, 2011 at 5:15 AM

Yes Currently we are working on revamping the whole data access and domain model architecture.

EF4.1 Code First is currently being the core data access for Entity Framework. Later will add NH3.

Currently using recent CTP to gain be able to have LINQ queries auto-compiled.

Sep 14, 2011 at 1:25 AM

Great.  My only reservation is the CPT.  The current CPT seems to have some unresolved issues with VS2010. 

I may want to wait the next release before I can try the CPT.


Sep 14, 2011 at 4:34 PM
JayKnowsit wrote:

Great.  My only reservation is the CPT.  The current CPT seems to have some unresolved issues with VS2010. 

I may want to wait the next release before I can try the CPT.


Well I'm doing some integration tests as I go. So far no issues. You can download the source code. Setup a database, change app.config of the integration test and check it out. Didn't dig too deep though.