0

I am trying to initialize the EFUnitOfWorkFactory object context.

In global.asax:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    ObjectFactory.Initialize(x =>
        {
            x.For<IUnitOfWorkFactory>().Use<EFUnitOfWorkFactory>();
            x.For(typeof(IRepository<>)).Use(typeof(EFRepository<>));
        }
    );
    EFUnitOfWorkFactory.SetObjectContext(() => new MyEntities());
}

In the generated Object context I have:

public partial class MyEntities : ObjectContext
{
    .....
    .....
}

I get an exception when I execute the web application in EFUnitOfWorkFactory.SetObjectContext(() => new MyEntities());:

Compiler Error Message: CS0012: The type 'System.Data.Objects.ObjectContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I added System.Data.Entity to my web application twice and rebuild but I still get this message. In addition, this line in the gobal.asax smetimes marked with red underline.

Does anyone knows how to solve this?

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
Naor
  • 21,508
  • 42
  • 135
  • 250
  • It looks like you are trying to create global object context. If I'm right you should check this answer: http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392 and possibly all linked questions in the right column to see what problems can you have if using global long living context. Context should be created per request or per "logical operation". – Ladislav Mrnka Apr 17 '11 at 13:25

1 Answers1

5

Are you sure that you have System.Data.Entity.dll from .NET 4.0 in your referenced assemblies in the web application or web site? It should also appear in web.config:

<system.web>
  <compilation debug="true" targetFramework="4.0">
    <assemblies>
      <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
  </compilation>

  ...
</system.web>
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • 1
    I had the same problem. It was showing up in my VS list of references, but wasn't in the web.config. – Ryan Aug 16 '12 at 15:55
  • 1
    This worked for me too, but why? It was in my list of references in solution explorer. When I would set Copy Local = True it would also work, but that is a bad solution. – Kevin Kalitowski Feb 14 '13 at 18:37