【问题标题】:Navigation Properties in EntityFrameworkCore [duplicate]EntityFrameworkCore 中的导航属性
【发布时间】:2018-11-01 05:07:57
【问题描述】:

我正在使用实体框架核心。我想要 3 个表:Appoitments、Users 和 Roles。 “用户”表包含具有不同角色的应用程序成员。有没有办法让'Appoitments'表带有外键:'User'类型的ClientID和ConsultantID?

public class ApplicationUser : IdentityUser
{
    public List<Appointment> Appointments { get; set; }
}

public class Appointment
{
    public int AppointmentId {get;set;}
    public DateTime Date { get; set; }
    public int RoomNumber { get; set; }

    public ApplicationUser ConsultantId { get; set; }
    public ApplicationUser ClientId { get; set; }
}

我创建了 ApplicationUser 和 Appointment 类,并在 ApplicationDbContext 中添加了属性: 公共 DbSet 约会 { 获取;放; }。

但是当我尝试添加迁移时,数据包管理器抛出“(无法确定由'List'类型的导航属性'ApplicationUser.Appointments'表示的关系。

【问题讨论】:

  • 是的,我不明白为什么没有?你试过什么?有什么问题吗?
  • 我创建了 ApplicationUser 和 Appointment 类,并在 ApplicationDbContext 中添加了属性:public DbSet Appointments { get;放; }.
  • 看起来类似于this question
  • 创建两个集合导航属性并将它们映射到相应的参考导航属性。从导航属性名称中去掉Id 也很好。例如ApplicationUser.ClientAppointments -> Appointment.ClientApplication.User.ConsultantAppointments -> Appointment.Consultant

标签: c# entity-framework-core


【解决方案1】:

问题中没有足够的信息真正区分正在发生的事情..但我不明白为什么这样的事情不起作用:

public class Appointment
{
    public int AppointmentId {get;set;}
    public DateTime Date { get; set; }
    public int RoomNumber { get; set; }
    [ForeignKey("Consultant")]
    public int ConsultantId { get; set; }
    [ForeignKey("Client")]
    public int ClientId { get; set; }


    public ApplicationUser Consultant { get; set; }
    public ApplicationUser Client { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多