Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:10:59+00:00 2026-05-31T11:10:59+00:00

Assume we have a domain class public class Incident { [Key] public virtual int

  • 0

Assume we have a domain class

public class Incident
{
    [Key]
    public virtual int IncidentId { get; set; }

    [Display(Name = "Parent Incident")]
    public virtual Incident ParentIncident { get; set; }

    [Display(Name = "Related Claim")]
    public virtual Incident ClaimIncident { get; set; }

 }

Other properties are omitted for simplicity.

When I had just ParentIncident in place, everything worked fine. Now I have added ClaimIncident to the class. I am attempting to update my database using the Entity Framework 4.3 (PreRelease) Migrations option, but I am getting an error, that EF doesn’t know how to map Incident to Incident.

Why referencing the same class instance once is allowed per class, and when I introduce the second one, it suddenly has no clue of how to reference it? And how can I correct the model class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:11:00+00:00Added an answer on May 31, 2026 at 11:11 am

    This is what I think is happening. When you only had ParentIncident Code First was mapping this by convention as one end of a self-referencing one-to-many unidirectional independent association. Lots of jargon there–let me explain:

    • Self-referencing: as is apprarant from the question, the Indicent entity is at both ends of the relationship.
    • One-to-many: Each Indicent has a single ParentIncident and an Incident can be the parent for many Incidents.
    • Unidirectional: There is a navigation property from the Incident to its parent (ParentIncident) but no reverse collection navigation properties of child incidents.
    • Indpendent Association: Since there is no foreign key property in the class, EF creates an FK column in the database (called ParentIncident_IncidentId) but it isn’t mapped to any object property.

    Now, when you add the ClaimIncident property this changes the way Code First tries to map. It now creates a self-referencing one-to-one bidirectional FK association:

    • One-to-one because it sees two navigation properties from Incident to Incident and assumes that these are two sides of the same relationship. Since neither is a collection property, the relationship is one-to-one.
    • Bidirectional again because there is now a nvaigation property on each end of the relationship.
    • FK because the one-to-one relationship becomes PK to PK and EF always treats this as an FK relationship since the FK (which is a PK) is mapped.

    But Code First can’t figure out which side of this one-to-one relationship should be the principal end, and which should be the dependant end. And this does matter sometimes…so Code First throws.

    Okay, so I’m going to infer from the thread and your model that you don’t really want a one-to-one relationship here. (A one-to-one self-referencing PK to PK relationship is kind of pointless, so you could argue that Code First shouldn’t create it, but Code First isn’t that smart.)

    So you put the FKs in. Two things happen. First, Code First now assumes that these must be one-to-many relationships again. Second, Code First now has a name for the FK property and in turn uses that name to name the FK column in the database. But that name is not the same as the FK column already created for the independent associated–it’s not ParentIncident_IncidentId. This means that Migrations would have to drop this column and create a new one…resulting in data loss.

    To tell Code First that you really just want two self-referencing, unidirectional, one-to-many, independent associations, use this fluent mapping:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Incident>()
            .HasOptional(e => e.ParentIncident)
            .WithMany();
    
        modelBuilder.Entity<Incident>()
            .HasOptional(e => e.ClaimIncident)
            .WithMany();
    }
    

    Now Migrations should correctly update your database. Now, it must be said that you may want to consider changing to FK associations, in which case you either need to do some data motion in Migrations, just accept the data loss if there is any, or tell Code First to keep using the FK column name that it previously generated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
Assume the following schema class Person{ public int Id {get;set;} public virtual ICollection<Province> Provinces
Assume some domain and view objects (that have no common base class) public class
I have the following domain class: public class Product { public virtual Guid Id
Let's assume I have the following domain: public class Movie { public string Id
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
Assume I have a class foo, and wish to use a std::map to store
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.