1

I have the following code and don't know why this error is thrown.

 using (var context = new EntitiesPlesk())
       {
             /////Some Code
       } 

using (var context = new EntitiesLan())   // Error Line
       {
             /////Some Code
       } 

I am using .sdf Database file for edmx model (EntitiesLan) Please help me where i should change to get rid of this error....Thanks!

ARC
  • 1,061
  • 14
  • 33
  • With the given code, it's not visible where the ambiguity is. Where do you land, when you press F12 in Visual Studio (go to definition)? It normally lists all possible occurences. – Andreas H. Oct 03 '15 at 13:14
  • @AndreasH. yes it goes to its definition : public partial class RshotelEntitiesLan : DbContext { public RshotelEntitiesLan() : base("name=RshotelEntitiesLan") { } – ARC Oct 03 '15 at 13:16
  • @AndreasH. remember that i have bind this edmx model with a .sdf database file – ARC Oct 03 '15 at 13:18
  • Since it's a partial class, you made sure there is no further definition of a constructor? Maybe in another file? What happens, if you comment out or alter the constructor you found? The it should lead you to the 'second part' of the ambiguity. – Andreas H. Oct 03 '15 at 13:29

1 Answers1

5

The problem is not with your code, the problem actually is that you are trying to regenerate your edmx in higher version of EntityFramework. One possibility could you might be modifying your old project of VS2010 in VS2013.

With previous version of Entity Framework a model created with the EF Designer would generate a context that derived from ObjectContext and entity classes that derived from EntityObject.

Starting with EF4.1 we recommended swapping to a code generation template that generates a context deriving from DbContext and POCO entity classes.

In Visual Studio 2012 you get DbContext code generated by default for all new models created with the EF Designer. Existing models will continue to generate ObjectContext based code unless you decide to swap to the DbContext based code generator.

Source: MSDN

Solution

Sergey Berezovskiy has described the solution as following, in THIS SO post.

You should either use None code generation strategy for your .edmx file. Or remove MainModel.tt and MainModel.Context.tt templates, which generate model entities and context.

If you use Default code generation strategy, then entities and context will be generated into MainModel.Designer.cs file. That would be standard entities, inherited from EntityObject, context will be inherited of ObjectContext. With Entity Framework 5 we have POCO entities generation. And whole generation is done in T4 templates, which generate context, inherited from DbContext, and POCO entities without some base type (well, object only).

When you have both templates and enabled code generation in edmx designer, then two sets of entities will be generated. That's why you have names conflict.

You can find THIS SO post useful in order to clear your understanding and resolution of this issue.

Community
  • 1
  • 1
Zeeshan
  • 2,884
  • 3
  • 28
  • 47