21

我正在尝试使用 EF 4.1 的 RC 版本创建一个快速的 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 识别为 Addresses 表的 FK 吗?我错过了什么吗?

谢谢!

4

6 回答 6

21

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

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 的引用。

于 2011-03-18T13:09:54.480 回答
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 只能是“共享”主键。

于 2011-03-18T13:30:03.357 回答
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; }

}
于 2012-07-15T21:09:28.037 回答
5

有一篇好帖子:EF Code First CTP5 中的关联:第 2 部分 – 共享主键关联

http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx

于 2011-04-20T21:10:25.883 回答
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; }

于 2011-03-18T12:38:40.793 回答
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; }

}
于 2012-08-21T12:24:40.370 回答