适当的代码首先相关类/表的EF6结构 [英] Proper Code First EF6 structure for related classes/tables

查看:88
本文介绍了适当的代码首先相关类/表的EF6结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班。一个人类和一个标题类。

  public class Person 
{
[Key]
public int Id {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public int TitleTypeId {get;组; }
}

public class TitleType
{
public int Id {get;组; }
public string Name {get;组;
}

标题类包含先生,夫人,MS之类的内容。等等。



定义这些类的正确方法是什么,以便我可以使用Linq从TitleType表中调用所需的标题name? p>

ie person.Title.name

解决方案

  public class Person 
{
[Key]
public int Id {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public TitleType标题{get;组; }
}

EF将创建额外的列来自动处理外键。 p>

I have a two classes. A person class and a Title class.

public class Person
{
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int TitleTypeId { get; set; }
}

public class TitleType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

The title class contains things like "Mr., Mrs., MS.,etc.."

What is the proper way to define these classes so that I can use Linq to recall the desired title "name" from the TitleType table?

i.e. person.Title.name

解决方案

public class Person
{
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public TitleType Title { get; set; }
}

EF will create additional column to handle Foreign Key for you automatically.

这篇关于适当的代码首先相关类/表的EF6结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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