简体   繁体   中英

Why Is A Navigation Property Updating Another Entity When Executing An Update In My DataContext? (C# Entity Framework)

I'm running into a situation using the Entity Framework (EF) that has me totally stumped. I'm doing a simple update and the error I'm getting is

Violation of PRIMARY KEY constraint 'PK__tblProducts_Mark__03E07F87'. Cannot insert duplicate key in object 'healthc.tblProducts_MarketSegmentGroups'. The statement has been terminated.

Let me give you some background on the problem.

I am using Web Forms and have a button click event fire to save some data in several text box controls on my page.

I have a table in my database called tblMetaProducts, which is a table used to store product information from various vendors we work with. The entity for this table is called Products.

I have another table called tblTechAssessment, which holds data for technical questions about the vendor's product, (eg what operating system can the software run on, version number etc.). The entity for this table is called TechnicalAssessment. A product can have many technical assessments and they are related by the product id.

I finally have a lookup table in the database called tblProducts_MarketSegmentGroups, which holds a product id and another id (which we don't care about for this problem). The entity for this table is called ProductMarketSegmentGroup. a product can have many product market segment groups and they are releated by the product id.

EDMX屏幕截图

Here is the code I'm executing to perform the EF save

private void UpdateTechnicalAssessments(int productID)
{
    var technicalAssessments = VendorDirectoryController.GetTechnicalAssessments(productID);
    var technicalAssessmentTypes = Enum.GetValues(typeof(TechnicalAssessmentType)).Cast<TechnicalAssessmentType>();
    foreach (var technicalAssessmentType in technicalAssessmentTypes)
    {
        var typeName = technicalAssessmentType.ToString();
        var id = "SaveToProduction" + typeName + "TextBox";
        var results = ProductInformationPanel.FindDescendantsByType<TextBox>().Single(x => x.ID == id).Text;
        technicalAssessments.Single(x => x.QuestionID == (int)technicalAssessmentType).Results = results;
    }

    VendorDirectoryController.SaveChanges();            
}

The SaveChanges() method drills down to my domain layer and calls the dataContext.SaveChanges() method.

So my questions are:

1) What can I do to get this to save my TechnicalAssessment entities? 2) Why does my save affect the ProductMarketSegmentGroup entity?

You might be hitting a bug in EF. I also stubmled on something similar (even though I use stored procedures).

The solution was to apply hotfix mentioned in: hotfix: Principal entity in an SQL application generates unnecessary updates - do it still effect EF 4.3.1?

The solution I ended up using was to have our database admin create a proc for the update. I was never able to figure out why the navigation property was causing such a fuss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM