如何将外键映射到EF Code First中的不同表/列名称?

时间:2012-02-14 03:54:57

标签: .net entity-framework foreign-keys ef-code-first

如何使用EF Code First将User中的字段映射到LookUpDetail? (最好使用注释,而不是FluentAPI)

public class User
{
   public int? ProvStateId { get; set; }
   public LookUpDetail ProvState { get; set; }
   public int? CountryId { get; set; }
   public LookUpDetail Country { get; set; }
}
public class LookUpDetail
{
   public int LookUpDetailId { get; set; }
   public string Value { get; set; }
}

我希望ProvStateId和CountryId都映射到LookUpDetailId,并且能够在提取用户记录时填充ProvState和COuntry。

我试过

[ForeignKey("ProvStateId")]
public LookUpDetail ProvState { get; set; }

这似乎设置了DB中的字段,但是     LookUpDetail ProvState 当我尝试提取用户记录时,它总是为空。

填充了LookUpDetail表,我可以很好地显示它的内容。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

您需要将Model类属性设置为虚拟属性,允许EF框架创建代理,并在延迟加载后为您加载相关对象。

(我不确定你是否进行延迟加载,如果不是你需要做明确的加载相关对象) 试试这个....

public class User
{
  public virtual int? ProvStateId { get; set; }
  public virtual LookUpDetail ProvState { get; set; }
  public virtual int? CountryId { get; set; }
  public virtual LookUpDetail Country { get; set; }
}

public class LookUpDetail
{
  public virtual int LookUpDetailId { get; set; }
  public virtual string Value { get; set; }
}

答案 1 :(得分:0)

这似乎有效:

public class User
{
    [Key]
    public string UserName { get; set; }
    public int? ProvStateId { get; set; }
    [ForeignKey("ProvStateId")]
    public LookUpDetail ProvState { get; set; }
    public int? CountryId { get; set; }
    [ForeignKey("CountryId")]
    public LookUpDetail Country { get; set; }
}

public class LookUpDetail
{
    [Key]
    public int LookUpDetailId { get; set; }
    public string Value { get; set; }
}

我已经使用以下测试它,它按预期输出和构造数据库:

    static void Main(string[] args)
    {
        var db = new TestContext();

        db.LookUpDetails.Add(new LookUpDetail {Value = "ProvState1"});
        db.LookUpDetails.Add(new LookUpDetail { Value = "ProvState2" });
        db.LookUpDetails.Add(new LookUpDetail { Value = "ProvState3" });
        db.LookUpDetails.Add(new LookUpDetail { Value = "ProvState4" });
        db.LookUpDetails.Add(new LookUpDetail { Value = "ProvState5" });
        db.LookUpDetails.Add(new LookUpDetail { Value = "Country1" });
        db.LookUpDetails.Add(new LookUpDetail { Value = "Country2" });

        db.SaveChanges();

        db.Users.Add(new User
                         {
                             UserName = "User1",
                             Country = db.LookUpDetails.Find(6),
                             ProvState = db.LookUpDetails.Find(1)
                         });

        db.Users.Add(new User
                         {
                             UserName = "User2",
                             Country = db.LookUpDetails.Find(7),
                             ProvState = db.LookUpDetails.Find(2)
                         });

        db.SaveChanges();

        foreach (User user in db.Users)
            Console.WriteLine(string.Format("\n\nUser Name: {0}, Prov State: {1}, Country: {2}", user.UserName,
                                            user.ProvState.Value, user.Country.Value));
        Console.ReadLine();
    }