Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core : nested entity foreign key not updating #25174

Closed
chin-udara opened this issue Jun 29, 2021 · 4 comments
Closed

EF Core : nested entity foreign key not updating #25174

chin-udara opened this issue Jun 29, 2021 · 4 comments

Comments

@chin-udara
Copy link

Assume this table structure of a database.

There are three model classes mapping each of these tables as follows;

public class TableOne
{
    public TableOne () 
    {
        tabletwo = new TableTwo();
        tablethree = new TableThree();
    }

    public int col1:1 { get; set; }
    public int col2:2 { get; set; }
    public int col3:3 { get; set; }

    public virtual TableTwo tabletwo { get; set; }
    public virtual TableThree tablethree { get; set; }
}

public class TableTwo
{
    public TableTwo () 
    {
        tablethree = new TableThree();
    }

    public int col2:1 { get; set; }
    public int col2:2 { get; set; }
    // ref => TableOne col 1:1
    public int col1:1 { get; set; }
    
    public virtual TableThree tableThree { get; set; }
}

public class TableThree
{
    public TableThree () 
    {
        tableOne = new TableOne();
        tableTwo = new TableTwo();
    }

    public int col3:1 { get; set; }
    // ref => TableTwo col 2:1
    public int col2:1 { get; set; }
    // ref => TableOne col 1:1
    public int col1:1 { get; set; }
    public TableOne tableOne { get; set; }
    public TableTwo tableTwo { get; set; }
}

Assume the references are properly set in DBContext. (Database first approach; assumption is EF properly scaffold-ed the Models and the relationships. )

The following code will populate model TableOne and add it the context.

TableOne one = new TableOne();
one.col1:1 = 1;
one.col1:2 = 2;
one.col1:3 = 3;
one.tabletwo.col2:1 = 4;
one.tabletwo.col2:2 = 5;
one.tabletwo.tableThree.col3:1 = 6;
DBContext.TableOne.Add(one);

Running this code, I get this error:

Cannot add or update a child row: a foreign key constraint fails (TableThree => col 1:1)

But this error does not happen when the relationship between TableThree and TableOne is removed.

Please consider the above code as a pseudo. The project is huge and I am trying to extract only the most relevant parts.

In short, the grand child, (in this case TableThree) does not get the col1:1 updated from the grand parent (in this case TableOne). But col2:1 in TableThree is properly updated with the Key from the immediate parent, TableTwo.

My understanding is that EF will update the child foreign keys automatically when inserting new entries. Is this not the case when there is a grand parent and parent relationship in a child entity?

Please help. Thank you!

@smitpatel
Copy link
Member

Related #18007

I believe the issue is initializing reference navigations in ctor. Specifically, when creating TableOne instance, instances for TableTwo & TableThree are created. And further TableTwo also creates a different instance of TableThree. Same way new instances are created for back navigations. This create inconsistent graph of objects on client side which does not match database schema. Can you remove initialization of reference navigation in ctor and see if issue reproduce?

@chin-udara
Copy link
Author

Thank you for your comment @smitpatel

I tried not initializing the navigation properties inside the ctor, still I get the foreign key reference error.

Cannot add or update a child row: a foreign key constraint fails

@smitpatel
Copy link
Member

@chin-udara - We will need a small runnable repro code to investigate this further.

@AndriySvyryd
Copy link
Member

EF Team Triage: This issue is lacking enough information for us to be able to effectively triage it. In particular, it is missing the following information requested in the new issue template. Can you please provide this information?

Steps to reproduce

Ideally include a complete code listing that we can run to reproduce the issue.

Alternatively, you can provide a project/solution that we can run.

BTW we're not just doing this to be mean 😄... we get a lot traffic on this project and it takes time to attempt to reproduce an issue based on fragments of information. In addition, our attempt is often unsuccessful as the exact conditions required to hit the issue are often not explicitly included in the code provided. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we ask that folks give us a self-contained way to reproduce an issue.

For a guide on submitting good bug reports, read Painless Bug Tracking.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants