当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - ASP.net Entity Framework Unable to determine the principal end of relationship

问题描述:

I was hoping someone could point me in the right direction here. I am building a c# ASP.net application with Entity Framework. I used the database first approach.

To keep things simple, assume I have 3 tables that are related. Report, a_report_vehicle and Vehicle. I have primary keys in each table. The a_report_vehicle has a foreign key to the Report table and Vehicle table.

So, I am attempting to relate many vehicles to a single report. I realize that I could simplify the design by adding the Report table key in the Vehicle table and eliminate the link table. However, the Vehicle table relates to other tables as well. So, I want to retain the a_report_vehicle link table.

Here is the most relevant code for this issue. Note that I have an object relating to the Report table, above this code. (newReport) I also have objects for newVehicle1 and newVehicle2.

Also note that if I add one vehicle with one row added to the a_report_vehicle table, this logic works fine. When I try to add 2 vehicle (2 separate Vehicle objects) with 2 rows in the link table, I receive an error 'Unable to determine principal end of relationship'.

I have looked at other posts and attempted to add dummy values to the vehicleID, but that did not work. In fact, if I set the first vehicleID to 1 and the second to 2, because those values actually exist in my Vehicle table, Entity Framework used those dummy values as if they were the real key values. Other posts led me to believe that Entity Framework just wants me to add placeholder values in the keys and it would resolve the real keys on database insert.

Here is the relevant code snippet. Any hints would be greatly appreciated.

 using (I.IE ctx = new I.IE()){

int rID, vID1, vID2;

ctx.t_Report.AddObject(newReport);

rID = newReport.ID;

ctx.t_Vehicle.AddObject(newVehicle1);

vID1 = newVehicle1.ID;

ctx.a_Report_Vehicle.AddObject(new a_Report_Vehicle

{

reportID = rID,

association_type = "text",

remarks = "Test",

DC = DateTime.Now,

UC = "3845",

vehicleID = vID1

});

ctx.t_Vehicle.AddObject(newVehicle2);

vID2 = newVehicle2.ID;

ctx.a_Report_Vehicle.AddObject(new a_Report_Vehicle

{

reportID = rID,

association_type = "text",

remarks = "Test",

DC = DateTime.Now,

UC = "3845",

vehicleID = vID2

});}

With the assistance of Gert, I managed to get 2 vehicle added. Here is the modified code that appears to work. (Add new a_Report_Vehicle object to the appropriate t_Vehicle navigation property Entity Collection and Entity Framework will manage the keys)

ctx.t_Vehicle.AddObject(newVehicle1);

vID1 = newVehicle1.ID;

newVehicle1.a_Report_Vehicle.Add(new a_Report_Vehicle

{

reportID = rID,

association_type = "text",

remarks = "Test",

DC = DateTime.Now,

UC = "3845",

vehicleID = vID1

});

ctx.t_Vehicle.AddObject(newVehicle2);

vID2 = newVehicle2.ID;

newVehicle2.a_Report_Vehicle.Add(new a_Report_Vehicle

{

reportID = rID,

association_type = "text",

remarks = "Test",

DC = DateTime.Now,

UC = "3845",

vehicleID = vID2

});

网友答案:

The problem is that you are assigning the key values of the new Vehicles to the foreign keys. But at that moment these values are still 0.

You should assign the Vehicle objects to the t_Vehicle navigation properties. Then EF will insert all objects in the required order to be able to fetch the generated PK values and use these for the new Report_Vehicle records.

The same applies to the new Report object.

网友答案:

I will take a stab at this and say that I believe the problem is that you are trying to use the id's of your inserted objects before they have actually been inserted into the database by a call to SaveChanges().

Try adding:

ctx.SaveChanges();

after each call to AddObject(); and before you try to get the id of the inserted object.

Before you call SaveChanges(), nothing has actually been saved to the database, and your auto-incremented id's have not actually been generated and returned to the entities yet.

分享给朋友:
您可能感兴趣的文章:
随机阅读: