gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:46:01 30 4
gpt4 key购买 nike

我正在尝试使用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时出现以下错误:

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识别为Addresses表的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; }
}

在第二实体中跳过对种族的引用。

关于asp.net-mvc-3 - EF 4.1-模型关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352059/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com