【问题标题】:MVC EF Code First one to one relationship errorMVC EF Code First 一对一关系错误
【发布时间】:2012-08-25 13:36:24
【问题描述】:

我想要一份展位清单(在贸易展览会上)和参展商清单。

展位列表与参展商列表分开 - 但是,一旦注册,我希望参展商能够预订展位。

当他们选择/预订展位时 - 我希望能够在我的视图中列出展位,并显示已预订的相关参展商。

同样,我想在另一个视图中列出参展商,以及他们预订的展位。

所以我正在尝试建立一对一的关系(使用 EF CodeFirst)。

但是,当尝试为展台或参展商添加控制器时,我收到以下错误:

我的模型是:

 public class Stand
{
    public int StandID { get; set; }
    public string Description { get; set; }
    public bool Booked { get; set; }
    public int ExhibitorID { get; set; }
    public virtual Exhibitor Exhibitor { get; set; }

}

 public class Exhibitor
{
    public int ExhibitorID { get; set; }
    public string Company { get; set; }
    public int StandID { get; set; }
    public virtual Stand Stand { get; set; }

}

我确定这与模型的“虚拟”部分有关。

谁能帮忙指出应该更新什么,以允许连接?

谢谢,

标记

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 entity-framework model


    【解决方案1】:

    EF 不知道哪个实体是主体(父),哪个是从属(子)。您需要在应该首先出现的实体上声明一个外键。您可以使用注释或流畅的映射来做到这一点。

    注释

    添加以下命名空间:

    using System.ComponentModel.DataAnnotations.Schema;
    

    使用以下注释注释您的 Stand 类:

    public class Stand
    {
        [ForeignKey("Exhibitor")]
        public int StandID { get; set; }
        public string Description { get; set; }
        public bool Booked { get; set; }
        public int ExhibitorID { get; set; }
        public virtual Exhibitor Exhibitor { get; set; }
    
    }
    

    Fluent 映射

    DbContext 类中覆盖 OnModelCreating 方法以包含:

    modelBuilder.Entity<Stand>()
        .HasOptional(s => s.Exhibitor)
        .WithRequired(e => e.Stand);
    

    【讨论】:

      【解决方案2】:

      您创建的模型无法用于关系数据库。 Stand 需要 ExibitorIdExibitor 需要 StandId。循环关系不允许您向任一表插入任何行。

      假设Exibitor 可能有多个Stand,将关系转换为一对多是一种选择。

      public class Stand
      {
          public int StandID { get; set; }
          public string Description { get; set; }
          public bool Booked { get; set; }
          public int? ExhibitorID { get; set; }
          public virtual Exhibitor Exhibitor { get; set; }    
      }
      
      public class Exhibitor
      {
          public int ExhibitorID { get; set; }
          public string Company { get; set; }
          public virtual ICollection<Stand> Stands { get; set; }
      }
      

      或者您可以使用共享主键映射来建立一对一的关系。其中Stand 是主要实体。 Exibitor 将使用 StandID 作为其 PK。

      public class Stand
      {
          public int StandID { get; set; }
          public string Description { get; set; }
          public bool Booked { get; set; }
          public virtual Exhibitor Exhibitor { get; set; }
      }
      
      public class Exhibitor
      {
          public int ExhibitorID { get; set; }
          public string Company { get; set; }
          public virtual Stand Stand { get; set; }
      }
      

      使用 Fluent API 配置关系。

      modelBuilder.Entity<Exibitor>().HasRequired(e => e.Stand)
          .WithOptional(s => s.Exibitor);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-18
        相关资源
        最近更新 更多