简体   繁体   中英

How to announce the principal end of an 1:1 association in Entity framework using data annotations

How do I announce the principal end of a 1:1 association in Entity framework using data annotations that are not Primary keys?

public class Event
{
    public Guid ID { get; set; }
    public DateTime Start { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Stop { get { return Start.Add(Duration); } }
    public string Title { get; set; }

    public Guid? ScheduleStartID { get; set; }
    public Guid? ScheduleCalculatedID { get; set; }
    public Guid? SchedAltAddID { get; set; }
    public Guid? SchedAltRemoveID { get; set; }

    [InverseProperty("StartEvent")]
    public virtual Schedule ScheduleStart { get; set; }
    [InverseProperty("CalculatedEvents")]
    public virtual Schedule ScheduleCalculated { get; set; }
    [InverseProperty("AddEvent")]
    public virtual ScheduleAlteration SchedAltAdd { get; set; }
    [InverseProperty("RemoveEvent")]
    public virtual ScheduleAlteration SchedAltRemove { get; set; }
}


public class ScheduleAlteration
{
    public Guid ID { get; set; }
    public Guid? AddEventID { get; set; }
    public Guid? RemoveEventID { get; set; }
    public Guid ScheduleID { get; set; }

    [InverseProperty("SchedAltAdd")]
    public virtual Event AddEvent { get; set; }
    [InverseProperty("SchedAltRemove")]
    public virtual Event  RemoveEvent { get; set; }
    public virtual Schedule Schedule { get; set; }
}

I get an error Unable to determine the principal end of an association between the types 'MLNSC.Models.JobEvent' and 'MLNSC.Models.ScheduleAlteration'

How do I specify that the Event is the principle object?

I found the answer here but not in the selected answer - that didn't work for me. The second answer suggested using the [required] data annotation.

[required, InverseProperty("SchedAltAdd")]
public virtual Event AddEvent { get; set; }

I upvoted both the question & the answer that worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM