简体   繁体   English

实体框架数据库优先配置

[英]Entity Framework Database First Configuration

I have the following tables in my database 我的数据库中有以下表格

Notes Table: 注释表:

  • NoteID 记事ID
  • Description 描述
  • TaskID (Can be NULL) TaskID(可以为NULL)

Task Table: 任务表:

  • TaskID TaskID
  • TaskType 任务类型
  • TaskDescription 任务描述

I an using Entity Framework 5.0 Database First approach. 我使用Entity Framework 5.0数据库优先方法。

In some cases there will be notes which linked to a single task but there will be cases that note are standalone that means that they not linked to single task. 在某些情况下,会有注释链接到单个任务,但是在某些情况下,注释是独立的,这意味着它们没有链接到单个任务。

My question is how do i need to configure the edmx (model) file so when i am asking for a single task he will give me the associated noted? 我的问题是我该如何配置edmx(模型)文件,因此当我要求执行单个任务时,他会给我相关的注释?

I think it something that i need to configure the mapping no? 我认为我需要配置映射吗?

You don't have to configure anything. 您无需配置任何内容。 Just gen the model from the db and then 只需从数据库生成模型,然后

 var query = context.Tasks.Include("Notes");

If you are doing code first then this will do it automatically. 如果您先编写代码,那么它将自动执行。 Simply make the TaskId nullable: 只需将TaskId设置为可空:

public class Note
{
    public int NoteID {get; set;}
    public string Description {get; set;}
    public int? TaskId {get; set;} // Notice the int is nullable

    public virtual Task {get; set;}
}

public class Task
{
    public int TaskID {get; set;}
    public TaskTypeEnum TaskType {get; set;}
    public string Description {get; set;}

    public virtual ICollection<Note> Notes {get; set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM