0
 [DataContract]
public class Match
{
    [DataMember]
    public Guid Id { get; set; }

    public virtual Tour Tour { get; set; }

    [DataMember]
    public DateTime DateMatch { get; set; }


    public virtual Team Home { get; set; }

    public virtual Team Guest { get; set; }

    public virtual Result Result { get; set; }
}

[DataContract]
public class Result
{
    [DataMember]
    public Guid Id { get; set; }

    public virtual Match Match { get; set; }
    public virtual List<Goal> Goals { get; set; }

}

I was trying to do this in Entity Framework when I got the error:

Unable to determine the principal end of an association between the types 'OperationWithTeams.Result' and 'OperationWithTeams.Match'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Venedchuk
  • 35
  • 6
  • but if delete public virtual Result Result { get; set; } working – Venedchuk Jul 01 '16 at 10:44
  • If you're using data annotations, add this to your `Match` class: `[ForeignKey("Result")] public Guid? ResultId { get; set; }` And to your `Result` class, add the following: `[ForeignKey("Match")] public Guid MatchId { get; set; }` This allows a match to have no result (NULL), if you don't want to allow that, make the `ResultId` non-nullable by removing the question mark (?). – Rudey Jul 01 '16 at 10:56

1 Answers1

0

Because your Model defines a circular reference between Match and Result, you must define the behavior you want explicitly in the ModelBuilder.

Assuming that a Result can not exist without a Match, but a Match can exist without a Result, make the Match the principal and Result the dependent:

ModelBuilder.Entity<Match>().HasOptional(m => m.Result).WithRequired(r => r.Match);
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36