如何在实体框架4 Entitydesigner中正确设置两个对象之间的关联?

时间:2022-03-17 21:30:12

For a new project I'm trying to create my business classes first and create the real database tables later. Therefore I'm using the Entity Framework 4 Designer. A created a new "ADO.Net Entity Data model" file, with the extension .edmx.

对于一个新项目,我尝试先创建我的业务类,然后创建真正的数据库表。因此我使用的是实体框架4设计器。A创造了一个新的“ADO”。Net实体数据模型“文件,扩展名。edmx。

I created two Entities:
如何在实体框架4 Entitydesigner中正确设置两个对象之间的关联?

我创建了两个实体:

I want to add a 1 to nc relation between Product -> Group. If I'd created the MSSQL database first, I would have added a column IDGroup to the Table Product and referenced Product.IDGroup to Group.IDGroup. As far as I can see, I can't add such association in the designer if I add a new Property called IDGroup to the Product Entity

我想在Product ->组之间增加一个1到nc关系。如果我首先创建了MSSQL数据库,那么我将向表产品和引用产品添加一个列IDGroup。IDGroup Group.IDGroup。就我所见,如果我向产品实体添加一个名为IDGroup的新属性,我就不能在设计器中添加这种关联

This is how I add the mapping: 如何在实体框架4 Entitydesigner中正确设置两个对象之间的关联?

我就是这样添加映射的:

Which results in:
如何在实体框架4 Entitydesigner中正确设置两个对象之间的关联?

结果:

Now the part what this question is about: If I add two tables from an existing MSSQL database to the edmx file, I'll get the compile error:

现在问题的关键是:如果我将现有MSSQL数据库中的两个表添加到edmx文件中,我将得到编译错误:

Error 3027: No mapping specified for the following EntitySet/AssociationSet - GroupSet, ProductSet

What does that error mean and what must I do to fix this? If I delete those two tables, I'll receive a warning instead:

这个错误是什么意思,我该怎么做才能修正呢?如果我删除这两个表,我将收到一个警告:

Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer myContainer.

Something tells me, I'm doing this all wrong and this is just basic stuff. How can I do it right?

有些东西告诉我,我做错了,这只是基本的东西。我该怎么做呢?

8 个解决方案

#1


85  

I just ran into this myself & when I Googled it (I hit your question).

我刚遇到这个问题&当我用谷歌搜索它时(我找到了你的问题)。

I was able to double click on the line (association line) in the designer. Fill in the properties there and I got it working.

我可以双击设计器中的行(关联行)。把属性填进去,我让它工作。

I also had to close VS and re-open it to get some of the errors reported to go away...

我还必须关闭VS并重新打开它,以使报告的一些错误消失……

Can't say this is the correct answer - just fumbling around and seeming to get results.

不能说这是正确的答案——只是胡乱摸索,似乎就能得到结果。

#2


9  

Something that might be easy to miss is to Generate DataBase from model. From what I understand this has to be run after every change made to the Database .edmx.

从模型中生成数据库很容易被忽略。根据我的理解,这必须在对数据库.edmx的每次更改之后运行。

This simple mistake happened to me and lead to frustration for a few minutes =)

这个简单的错误发生在我身上,让我沮丧了几分钟。

#3


6  

I got this error when I generated the model from the database using the ADO.Net DBContext Generator under VS2010 SP1 with EF4.1 installed. The database I was using did NOT have any referential integrity between the primary and foreign key fields.

当我使用ADO从数据库生成模型时,我得到了这个错误。安装了EF4.1的VS2010 SP1下的Net DBContext生成器。我使用的数据库在主键字段和外键字段之间没有任何引用完整性。

The solution was to double click the association line which brings up the Referential Contstrainst dialog box. I then set the Principal field to be the table with the primary key, the Dependent as the table with the foreign key, set the foreign key names to match in the drop down and clicked OK.

解决方案是双击关联行,该关联行会弹出引用Contstrainst对话框。然后,我将主体字段设置为具有主键的表,依赖项设置为具有外键的表,并在下拉菜单中设置要匹配的外键名并单击OK。

The code then compiled and I was able to work across the entities using var foreign = DBContext.Primary.First().Foreign etc

然后编译代码,我可以使用var foreign = dbcontext.primar. first()跨实体工作。外国等

Steve

史蒂夫

#4


3  

I had the same error, and one bit of configuration that the "Add Association" dialog didn't help me set was the "Referential Constraint" property. Once I configured that I rebuilt the solution and everything was cool.

我也有同样的错误,“添加关联”对话框没有帮助我设置的一个配置是“引用约束”属性。一旦我配置好重新构建了解决方案,一切都很好。

#5


0  

I've had this error when I've deleted an association from the model diagram but the association remains in the underlying XML in the .edmx. They only way I've found to fix that is to hand-edit the edmx to remove references to the offending association.

当我从模型图中删除关联时,我犯了这个错误,但是关联仍然在.edmx中的底层XML中。我找到的解决方法是手工编辑edmx,以删除对违规关联的引用。

#6


0  

All you just have to do is delete all your tables from edmx file. Now right click in edmx file and choose update form database option. Update Ef, issue will be resolved.

只需从edmx文件中删除所有表。现在在edmx文件中右键单击并选择update form database选项。更新Ef,问题将得到解决。

#7


0  

I am using database first approach and encountered the same issue.

我正在使用数据库优先方法,遇到了同样的问题。

what was causing the problem is on of my coworker deleted one of the table from the database and forgot to update the model.

导致问题的是我的同事从数据库中删除了一个表,并忘记更新模型。

My solution is find the deleted entity from the diagram. (model browser could be useful.) Then delete it from the diagram, since EF doesn't remove entities if you deleted the table from the database.

我的解决方案是从图中查找已删除的实体。(模型浏览器可能有用。)然后从图中删除它,因为如果从数据库中删除表,EF不会删除实体。

Build, and check Error List window (Ctrl+E,W).

构建并检查错误列表窗口(Ctrl+E,W)。

Done.

完成了。

#8


0  

I have also found that rebuilding your project after changes to your Entity Framework context and models can sometimes resolve these errors, especially if the Entity Framework contexts are in a different project.

我还发现,在更改实体框架上下文和模型之后重新构建项目有时可以解决这些错误,特别是如果实体框架上下文位于不同的项目中。

#1


85  

I just ran into this myself & when I Googled it (I hit your question).

我刚遇到这个问题&当我用谷歌搜索它时(我找到了你的问题)。

I was able to double click on the line (association line) in the designer. Fill in the properties there and I got it working.

我可以双击设计器中的行(关联行)。把属性填进去,我让它工作。

I also had to close VS and re-open it to get some of the errors reported to go away...

我还必须关闭VS并重新打开它,以使报告的一些错误消失……

Can't say this is the correct answer - just fumbling around and seeming to get results.

不能说这是正确的答案——只是胡乱摸索,似乎就能得到结果。

#2


9  

Something that might be easy to miss is to Generate DataBase from model. From what I understand this has to be run after every change made to the Database .edmx.

从模型中生成数据库很容易被忽略。根据我的理解,这必须在对数据库.edmx的每次更改之后运行。

This simple mistake happened to me and lead to frustration for a few minutes =)

这个简单的错误发生在我身上,让我沮丧了几分钟。

#3


6  

I got this error when I generated the model from the database using the ADO.Net DBContext Generator under VS2010 SP1 with EF4.1 installed. The database I was using did NOT have any referential integrity between the primary and foreign key fields.

当我使用ADO从数据库生成模型时,我得到了这个错误。安装了EF4.1的VS2010 SP1下的Net DBContext生成器。我使用的数据库在主键字段和外键字段之间没有任何引用完整性。

The solution was to double click the association line which brings up the Referential Contstrainst dialog box. I then set the Principal field to be the table with the primary key, the Dependent as the table with the foreign key, set the foreign key names to match in the drop down and clicked OK.

解决方案是双击关联行,该关联行会弹出引用Contstrainst对话框。然后,我将主体字段设置为具有主键的表,依赖项设置为具有外键的表,并在下拉菜单中设置要匹配的外键名并单击OK。

The code then compiled and I was able to work across the entities using var foreign = DBContext.Primary.First().Foreign etc

然后编译代码,我可以使用var foreign = dbcontext.primar. first()跨实体工作。外国等

Steve

史蒂夫

#4


3  

I had the same error, and one bit of configuration that the "Add Association" dialog didn't help me set was the "Referential Constraint" property. Once I configured that I rebuilt the solution and everything was cool.

我也有同样的错误,“添加关联”对话框没有帮助我设置的一个配置是“引用约束”属性。一旦我配置好重新构建了解决方案,一切都很好。

#5


0  

I've had this error when I've deleted an association from the model diagram but the association remains in the underlying XML in the .edmx. They only way I've found to fix that is to hand-edit the edmx to remove references to the offending association.

当我从模型图中删除关联时,我犯了这个错误,但是关联仍然在.edmx中的底层XML中。我找到的解决方法是手工编辑edmx,以删除对违规关联的引用。

#6


0  

All you just have to do is delete all your tables from edmx file. Now right click in edmx file and choose update form database option. Update Ef, issue will be resolved.

只需从edmx文件中删除所有表。现在在edmx文件中右键单击并选择update form database选项。更新Ef,问题将得到解决。

#7


0  

I am using database first approach and encountered the same issue.

我正在使用数据库优先方法,遇到了同样的问题。

what was causing the problem is on of my coworker deleted one of the table from the database and forgot to update the model.

导致问题的是我的同事从数据库中删除了一个表,并忘记更新模型。

My solution is find the deleted entity from the diagram. (model browser could be useful.) Then delete it from the diagram, since EF doesn't remove entities if you deleted the table from the database.

我的解决方案是从图中查找已删除的实体。(模型浏览器可能有用。)然后从图中删除它,因为如果从数据库中删除表,EF不会删除实体。

Build, and check Error List window (Ctrl+E,W).

构建并检查错误列表窗口(Ctrl+E,W)。

Done.

完成了。

#8


0  

I have also found that rebuilding your project after changes to your Entity Framework context and models can sometimes resolve these errors, especially if the Entity Framework contexts are in a different project.

我还发现,在更改实体框架上下文和模型之后重新构建项目有时可以解决这些错误,特别是如果实体框架上下文位于不同的项目中。