Monday 15 March 2010

c# - The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations -


"The main interval of this collaboration should be clearly configured via the relative API or data annotation relation."

I get this error in Entity Framework 4.4 while updating / migrating the database, but I am not trying to specify 1: 1 relationship. I want something like this:

  Public class EntityA {public int id {get; Set; } {EntityBID} Public Entity; Set; } [Foreign key ("Antibid")] Public Virtual EntityB EntityB {get; Set; }} Public category EntityB {public int id {get; Set; } Public taps qualified & lt; Int & gt; Favorite antidepressa {receive; Set; } [Foreign ("PreferredTTAd")] Public Virtual Unit A PriorityAntAa {Received; Set; }}   

Where EntityA should have an EntityB original, while EntityB may be the favorite EntityA child, but this does not have to be one of the favorite children to be connected to parents Should, but I do not know how to implement it in the database. I am planning to implement programming.

How do I get this error or what is a better way to fulfill these relationships? Entity Framework Code-First Conventions Believe that EntityA.EntityB and

EntityB.PreferredEntityA are related to the same relationship and each other has inverted navigation properties. Because both navigation properties are references (not archive) EF estimates one-to-one relationship.

Since you really want two to-one relationships, so you have to override conferences, with your model it is possible only with the Fluent API:

  Modelbuilder.Entity & lt; EntityA & gt; () .HasRequired (a => a.EntityB) with .any () HesForeignKey (a => a.EntityBID); Modelbuilder.Entity & lt; EntityB & gt; () .HasOptional (b = & gt; B. PreferredEntityA) .Many (). HesForeignKey (b = & gt; B. PreferredEntityAID);   

(You can remove [ForeignKey] attributes if you use it.)

You do not specify a mapping Who can ensure that favorite children are always one of the respected children.

If you do not want to use the Fluent API, but only the data annotation you can add the archive property to EntityB and it can relate to EntityA.EntityB < / Code> using [InverseProperty] attribute:

  public class EntityB {Public Ent ID {Received; Set; } Public taps qualified & lt; Int & gt; Favorite antidepressa {receive; Set; } [Foreign ("PreferredTTAd")] Public Virtual Unit A PriorityAntAa {Received; Set; } [InverseProperty ("EntityB")] // & lt; - Navigation Property Name Public Virtual IcLonac in the ATTTI & lt; EntityA & gt; EntityAs {Receive; Set; }}    

No comments:

Post a Comment