C# Unable to determine the principal end of the relationship

后端 未结 2 1797
逝去的感伤
逝去的感伤 2021-02-05 01:20
foreach (var item in order.MyFiles)
{
   var newFile = adapter.db.File.CreateObject();

   newFile.Name = item.FileName;

   adapter.db.File.AddObject(newFile);

   adap         


        
2条回答
  •  不知归路
    2021-02-05 02:16

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

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

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

    where File is the navigation property defined in MyFile entity.

提交回复
热议问题