如何使用数据注释在Entity框架中宣布1:1关联的主端

本文关键字:关联 数据 何使用 注释 框架 Entity | 更新日期: 2023-09-27 18:25:41

如何使用非主键的数据注释在Entity框架中宣布1:1关联的主体端?

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; }
}

我出错了无法确定类型"MLNSC.Models.JobEvent"answers"MLNSC.Models.ScheduleAlteration"之间关联的主体端

如何指定事件为主要对象?

如何使用数据注释在Entity框架中宣布1:1关联的主端

我在这里找到了答案,但在选定的答案中没有找到——这对我不起作用。第二个答案建议使用[必需]数据注释。

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

我对这两个问题都投了赞成票;对我有效的答案。