C# 无法确定关系的主体端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11893673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:39:14  来源:igfitidea点击:

C# Unable to determine the principal end of the relationship

c#entity-framework

提问by Tony

foreach (var item in order.MyFiles)
{
   var newFile = adapter.db.File.CreateObject();

   newFile.Name = item.FileName;

   adapter.db.File.AddObject(newFile);

   adapter.db.SaveChanges();

   item.MyFile.Add(new MyFile { FileID = newFile.FileID });

   adapter.db.SaveChanges();
}

foreach (var item in tempFilesList)
{
    adapter.db.DeleteObject(item);
}

adapter.db.SaveChanges();

That code duplicates rows in the MyFiletable, e.g if the loop iterates 3 times I see 6 rows (3 x 2*adapter.db.SaveChanges()???)

该代码复制MyFile表中的行,例如,如果循环迭代 3 次,我会看到 6 行(3 x 2* adapter.db.SaveChanges()???)

But, if I just have only one adapter.db.SaveChanges();(that last one) I get the error

但是,如果我只有一个adapter.db.SaveChanges();(最后一个),我会收到错误消息

Unable to determine the principal end of the 'my_dbModel.FK_MyFile_File' relationship. Multiple added entities may have the same primary key.

无法确定“my_dbModel.FK_MyFile_File”关系的主体端。多个添加的实体可能具有相同的主键。

I suppose it is caused that in that case it doesn't commit the adapter.db.File.AddObject(newFile);items before assinging them to the item.MyFile.Add(new MyFile { FileID = newFile.FileID });But I can be wrong, any ideas how to fix it?

我想这是因为在这种情况下它不会在将adapter.db.File.AddObject(newFile);项目分配给它们之前提交项目item.MyFile.Add(new MyFile { FileID = newFile.FileID });但我可能是错的,任何想法如何解决它?

采纳答案by Kamyar

You cannot use newFile.FileIDwhen defining a new MyFilebefore saving changes. The FileID is default (0) until you save the new entity in database. You'd have to use navigation property of Filein your MyFileclass. EF will detect the relation and will commit data appropriately.

在保存更改之前newFile.FileID定义新时不能使用MyFile。FileID 为默认值 (0),直到您将新实体保存在数据库中。你必须File在你的MyFile班级中使用导航属性。EF 将检测关系并适当提交数据。

Try to change the line item.MyFile.Add(new MyFile { FileID = newFile.FileID });with:

尝试更改行item.MyFile.Add(new MyFile { FileID = newFile.FileID });

item.MyFile.Add(new MyFile { File = newFile });  

where Fileis the navigation property defined in MyFileentity.

实体中File定义的导航属性在哪里MyFile

回答by user3285693

This could be because of a cyclic reference in you EDMX back to the same table.

这可能是因为 EDMX 中的循环引用返回到同一个表。

Means for example if EmployeeDepartment is the table that you are going to update and the Primary key is EmployeeDepartmentID and if it is identity column and auto-generated, Check in the EDMX whether the EmployeeDepartmentID is referenced back to itself. If so, right click on that foreigh key reference and click delete.

例如,如果 EmployeeDepartment 是您要更新的表并且主键是 EmployeeDepartmentID 并且它是身份列并且是自动生成的,则在 EDMX 中检查 EmployeeDepartmentID 是否引用回自身。如果是这样,请右键单击该外键引用并单击删除。

This worked for me and I hope this works for you as well.

这对我有用,我希望这对你也有用。

Thanks,

谢谢,

Bibin.

比宾。