首页 文章

EF 4.1 - 模型关系

提问于
浏览
21

我正在尝试使用RC版本的RS 4.1创建一个快速的ASP.NET MVC 3应用程序 . 我有两个型号:

public class Race
{
    public int RaceId { get; set; }
    public string RaceName { get; set; }
    public string RaceDescription { get; set; }
    public DateTime? RaceDate { get; set; }
    public decimal? Budget { get; set; }
    public Guid? UserId { get; set; }
    public int? AddressId { get; set; }

    public virtual Address Address { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string StreetCont { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public virtual Race Race { get; set; }
}

尝试插入新Race时出现以下错误:

无法确定类型'rcommander.Models.Race'和'rcommander.Models.Address'之间关联的主要结束 . 必须使用关系流畅API或数据注释显式配置此关联的主要结尾 .

它不应该自动将RaceId识别为Races表的主键和AddressId作为地址表的FK吗?我错过了什么吗?

谢谢!

6 回答

  • 0

    这里的问题似乎是EntityFramework无法识别foreing键的位置,因为您在两个对象中都持有交叉引用 . 不确定你想要实现什么,我可能会建议这样的事情:

    public class Race
    {
      public int RaceId { get; set; }
      public string RaceName { get; set; }
      public string RaceDescription { get; set; }
      public DateTime? RaceDate { get; set; }
      public decimal? Budget { get; set; }
      public Guid? UserId { get; set; }
    
      public int? AddressId { get; set; }
      public virtual Address Address { get; set; }
    }
    
    public class Address
    {
      public int AddressId { get; set; }
      public string Street { get; set; }
      public string StreetCont { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string ZipCode { get; set; }
    }
    

    在第二个实体中跳过对Race的引用 .

  • 8

    这里的问题是地址和种族之间的1:1关系!您可能希望将其映射为1:N,因此您需要将地址修改为:

    public class Address
    {
      public int AddressId { get; set; }
      public string Street { get; set; }
      public string StreetCont { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string ZipCode { get; set; }
    
      public virtual ICollection<Race> Races { ... }
    }
    

    如果你想使用1:1然后你不能在Race中使用AddressId但Address中的AddressId必须是Race的外键,因为实体框架只能以1:1的方式“共享”主键 .

  • 21

    对于一对一关系,您需要在第二个类中添加“[required]”属性 . 见下文:

    public class Race
    {
      public int RaceId { get; set; }
      public string RaceName { get; set; }
      public string RaceDescription { get; set; }
      public DateTime? RaceDate { get; set; }
      public decimal? Budget { get; set; }
      public Guid? UserId { get; set; }
    
      public int? AddressId { get; set; }
      public virtual Address Address { get; set; }
     }
    
    public class Address
    {
     public int AddressId { get; set; }
     public string Street { get; set; }
     public string StreetCont { get; set; }
     public string City { get; set; }
     public string State { get; set; }
     public string ZipCode { get; set; }
    
     [required]
     public Race Race { get; set; }
    
    }
    
  • 4
  • 18

    它按惯例将Id视为主键 . 那么你需要做什么:

    public class Race
    {
        [Key]
        public int RaceId { get; set; }
        public string RaceName { get; set; }
        public string RaceDescription { get; set; }
        public DateTime? RaceDate { get; set; }
        public decimal? Budget { get; set; }
        public Guid? UserId { get; set; }
        public int? AddressId { get; set; }
    
        public virtual Address Address { get; set; }
    }
    and
    
    public class Address
    {
        [Key]
        public int AddressId { get; set; }
        public string Street { get; set; }
        public string StreetCont { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    
        [ForeignKey("RaceId")] // Maybe telling it what the ForeignKey is will help?
        public virtual Race Race { get; set; }
    }
    

    [Key] 属性表示它应该是PrimaryKey

    如果您不想这样,则需要将主键重命名为 public int Id {get; set; }

  • 5

    我认为它也会像这样解决......我认为地址不需要与种族相关联,但种族必须始终与地址相关联 . 我和患者和事故有同样的问题,我用InverseProperty解决了这个问题,实际上与外键相同,但另一个方向

    public class Race
    {
      public int RaceId { get; set; }
      public string RaceName { get; set; }
      public string RaceDescription { get; set; }
      public DateTime? RaceDate { get; set; }
      public decimal? Budget { get; set; }
      public Guid? UserId { get; set; }
    
      public int AddressId { get; set; }
    
      [ForeignKey("AddressId")]
      public virtual Address Address { get; set; }
     }
    
    public class Address
    {
     public int AddressId { get; set; }
     public string Street { get; set; }
     public string StreetCont { get; set; }
     public string City { get; set; }
     public string State { get; set; }
     public string ZipCode { get; set; }
    
     public int? RaceId { get; set; }
     [InverseProperty("RaceId")]
     public Race Race { get; set; }
    
    }
    

相关问题