EF 4.1 - 模型关系 [英] EF 4.1 - Model Relationships

本文介绍了EF 4.1 - 模型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用EF 4.1 RC版快速ASP.NET MVC 3应用程序。我有两个型号:

I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models:

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

试图插入一个新的种族,当我收到以下错误:

I get the following error when trying to insert a new Race:

无法确定到底本金
  类型之间的关联的
  rcommander.Models.Race和
  rcommander.Models.Address。该
  该协会的首席端部应
  使用明确配置无论是
  关系流利的API或数据
  注释。

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作为种族表的主键和AddressId为FK自动将地址表?我缺少的东西吗?

Shouldn't it recognize RaceId as the primary key of the Races table and AddressId as the FK to the Addresses table automatically? Am I missing something?

谢谢!

推荐答案

这里的问题似乎是的EntityFramework无法识别其中foreing关键的是,因为你持有两个对象的交叉引用。不是确定你想要什么来实现的,我建议是这样的:

The problem here seems to be that EntityFramework can't recognize where the foreing key is, as you are holding cross references in both objects. Not being sure what you want to achieve, I may suggest something like this:

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

跳绳参考第二实体的比赛。

Skipping reference to Race in second entity.

这篇关于EF 4.1 - 模型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆