加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – EF 4.1 – 模型关系

发布时间:2020-12-15 18:40:46 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用RC 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
我正在尝试使用RC 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; }
}

尝试插入新的比赛时,我收到以下错误:

Unable to determine the principal end
of an association between the types
‘rcommander.Models.Race’ and
‘rcommander.Models.Address’. The
principal end of this association must
be explicitly configured using either
the relationship fluent API or data
annotations.

不应该将RaceId作为Races表和AddressId的主键识别为地址表的FK吗?我缺少什么?

谢谢!

解决方法

这里的问题似乎是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; }
}

在第二个实体中跳过参考。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读