EF 4.1 - 模型关系

时间:2011-03-18 12:36:52

标签: asp.net-mvc-3 entity-framework-4 entity-relationship ef-code-first entity-framework-4.1

我正在尝试使用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; }
}

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

  

无法确定主要目标   类型之间的关联   'rcommander.Models.Race'和   'rcommander.Models.Address'。该   这个协会的主要结束必须   使用其中任何一个显式配置   关系流畅的API或数据   注释

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

谢谢!

6 个答案:

答案 0 :(得分:21)

这里的问题似乎是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的引用。

答案 1 :(得分:18)

这里的问题是地址和种族之间的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的方式“共享”主键。

答案 2 :(得分:8)

对于一对一关系,您需要在第二个类中添加“[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; }

}

答案 3 :(得分:5)

答案 4 :(得分:4)

它按惯例将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 :(得分:0)

我认为它也会像这样解决......我认为地址不需要与种族相关联,但种族必须始终与地址相关联。 我和患者和事故有同样的问题,我用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; }

}
相关问题