0

When I use a simple Master - Detail relationship

public class Master
{
  public long ID { get; set; }
  public virtual Detail Detail { get; set; }
}

public class Detail
{
  public long ID { get; set; }
}

Code First creates the data model I would expect: The outer master table references the inner detail. When I change it in a way, that the Master references itself

public class Master
{
  public long ID { get; set; }
  public virtual Master Inner { get; set; }
}

Code First creates a data model where the inner master references the outer master. Why behaves Entity Framework different in that case? Can I correct this with data annotations (avoiding fluent api)?

StefanG
  • 1,234
  • 2
  • 22
  • 46
  • If I got your question correctlyI believe it could be done with InverseProperty annotation,for example http://stackoverflow.com/questions/5691780/navigation-property-without-declaring-foreign-key – hyperN Dec 18 '13 at 18:51
  • This may help http://msdn.microsoft.com/en-us/data/jj713564 and http://msdn.microsoft.com/en-us/data/jj591583 – phil soady Dec 19 '13 at 10:45
  • InverseProperty does not work. It seems only to work with ICollection. See http://stackoverflow.com/questions/18625631/ef-4-4-inverseproperty-does-not-quite-inverse – StefanG Dec 19 '13 at 13:25

1 Answers1

0

Entity Framework works correct. You can see this by adding a Value property:

public class Master
{
  public long ID { get; set; }
  **public string Value { get; set; }**
  public virtual Master Inner { get; set; }
}

All what Entity Framework does, is reverse the order of the objects. The root object has highest id whereas the most inner object is the one starting with ID = 0. (Thinking about it, it's quite obvious why EF does this).

So, I just misinterpreted the data.

StefanG
  • 1,234
  • 2
  • 22
  • 46