Search code examples
c#entity-frameworkdbcontextmember

asp.net Entity Framework 6.0 - What Context Generator should I be using?


Reworded the question:

I need to upgrade to the 6.0 Entity Framework. I used the EF 6.x Entity Object Generator to create a new Context. Now, I get a syntax error. It seems that the generated code now generates an ObjectContext rather than the newer DBContext.

Here's the code that no longer works.

public abstract class Base<T> where T : class {
    public static void save(T entity) {
        using (var context = new DataContext()) {
            context.Entry(entity).Member("Changed_Date").CurrentValue = DateTime.Now;
            context.Entry(entity).Member("Changed_User").CurrentValue = userId;
            context.SaveChanges();
        }
    }
}

The problem is: Entry (in the Base class) is now a compile error when used with the generated code.

The DataContext (snippets):

using System;
using System.ComponentModel;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

[assembly: EdmSchemaAttribute()]
#region EDM Relationship Metadata

#endregion

namespace DB
{
    public partial class encludeDataContext : ObjectContext
    {
    }
}

Which generator should I be using?


Solution

  • Some decoupling was performed in EF6.

    public DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class
    

    is in library System.Data.Entity

    System.Data.Entity is in BOTH EntityFramework and System.Data

    so becareful with namespaces

    Context.Entry(entity).Member("Changed_Date").   //.... should be Ok