EF 4.1 - 模型关系

Mik*_*ike 21 entity-relationship entity-framework-4 ef-code-first entity-framework-4.1 asp.net-mvc-3

我正在尝试使用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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢!

tpe*_*zek 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; }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 不会从地址中删除导航可能性吗? (4认同)

Lad*_*nka 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 { ... }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 您能否请更新您的答案,以显示如何使用共享主键1:1?我希望有一种双向访问方式:Race.Address和Address.Race.如果可能,请在示例中保留Race和Address.谢谢. (3认同)

小智 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; }

}
Run Code Online (Sandbox Code Playgroud)