14

I have 3 tables in my MS SQL database and I have added a EntityFramework(latest) to my project where I have imported these 3 tables. The first problem was that no Entities was built so I changed "Code Genereation Strategy" from None to Default.

After build I get

X Already Contains a definition for Y

on all properties of the entities.

When looking closer it have generated a partial ex Users.cs and one partial User in in the MainModel.Designer.cs?

Why is it generating User.cs? I have a similar setup in another project and the EF is set with the same settings, there is no User.cs?

Edit1 : I can see one thing that differs and thats Use Strong Spatial Types that is set to False in the failing project, it is however not possible to set it to true(grayed)?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Ivy
  • 2,285
  • 4
  • 24
  • 29

2 Answers2

22

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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks! If I switch the code generation to none I notice that the type will be generated as you say but It will be named Users just like the table in database. I need it to be called User as an entity in the solution. Is this possible to change with the POCO generation? I have tried to change the name of the table in EF but that does not work? It is often that I remove all entities in the EF and add them again, so it would be great if the solution works with that. – Ivy Dec 02 '12 at 11:35
  • Yes, it is very simple. Open edmx designer, select `Users` entity, and rename it to `User` (double click on etity title). Then save edmx file - `MainModel.Context.tt` will re-generated entity classes (you will see `User.cs` file instead of `Users.cs`). – Sergey Berezovskiy Dec 02 '12 at 11:55
  • Sorry but that does not work, it does only recognize Users even when the entity title is set to User. – Ivy Dec 02 '12 at 15:22
  • 1
    When using Linq against the edmx it does not rocognize the namespace that is set (MainModel)? Right now the esiest way is probably delete the tt files and activate Code Generation Strategy, is there any draw back to this? – Ivy Dec 02 '12 at 15:25
  • 1
    Thanks guys. was struggling without able to find the cause of the error. now got it. – Ebenezar John Paul Mar 06 '13 at 10:30
3
  1. Right Click the Entity model.
  2. Go to properties and remove the default name in the "Custom Tool".
  3. Assign the edmx in the model.tt and model.context
  4. Build and execute it you will be free from error.
Maxim Daigle
  • 393
  • 3
  • 8
Rajeshkumar
  • 75
  • 1
  • 2
  • 10