X Already contains a definition Y with EntityFrame

2020-02-10 04:08发布

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)?

2条回答
甜甜的少女心
2楼-- · 2020-02-10 04:24
  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.
查看更多
Juvenile、少年°
3楼-- · 2020-02-10 04:32

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.

查看更多
登录 后发表回答