0

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?

Stoertz
  • 7
  • 6
  • 1
    Please post the exact compiler error message! – nemesv Jan 03 '14 at 14:20
  • Thanks @nemesv: Error 493 'DataContext' does not contain a definition for 'Entry' and no extension method 'Entry' accepting a first argument of type 'DataContext' could be found (are you missing a using directive or an assembly reference?) – Stoertz Jan 03 '14 at 14:22
  • It's a normal member of `System.Data.Entity.DbContext`, check (and post) the definition of your DataContext. – H H Jan 03 '14 at 14:25
  • Check if your references are pointing to the correct binaries. – rkawano Jan 03 '14 at 14:31
  • The problem is that the DataContext is 482,160 lines long! It's the auto generated code from the edmx. But I've added a snippet to the original post. – Stoertz Jan 03 '14 at 14:43
  • I think it's a normal member of System.Data.Entity.DbContext in EF5 but seems not to be there in EF6. – Stoertz Jan 03 '14 at 14:49
  • AHAH: by looking more closely at the DataContext, I see that it is inherited from ObjectContext now rather than DBContext. This explains why Entry() is not a member... but does not answer the question, How do you do this in EF6? – Stoertz Jan 03 '14 at 14:55
  • DbContext is a wrapper (decorator) for code-first. Are you sure you were using an EDMX (db/model first) in EF5? – H H Jan 03 '14 at 15:43
  • If you are using EF6 remove the reference to System.Data.Entity.dll to avoid hard to debug errors resulting in using EF5 types in an EF6 app. – Pawel Jan 03 '14 at 17:19

1 Answers1

0

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
phil soady
  • 11,043
  • 5
  • 50
  • 95
  • True dat... I think I've resolved those issues. It seems I ought to have asked a more clear question... How do I get the generator to create a DBContext rather than an ObjectContext. – Stoertz Jan 03 '14 at 15:09
  • you have a model generated Context. ? So regenerate it under ef6 – phil soady Jan 03 '14 at 15:13
  • I think I'm using the EF 6.x EntityObject Generator. – Stoertz Jan 03 '14 at 15:19
  • im thinking question sample code and title dont match the actual issue. Should the question be "reworked"? – phil soady Jan 03 '14 at 15:23
  • I think you comments have led me closer to the real problem... I have probably used the wrong Context generator and should be using something else like Reverse POCO or CodeFirst. – Stoertz Jan 03 '14 at 15:34
  • well i cant comment on that, I have no idea what youre up to. Model first / Codefirst / DB first. And what this 482K long file is. Nor what generated it. – phil soady Jan 03 '14 at 15:38