简体   繁体   中英

Entity Framework Database First Configuration

I have the following tables in my database

Notes Table:

  • NoteID
  • Description
  • TaskID (Can be NULL)

Task Table:

  • TaskID
  • TaskType
  • TaskDescription

I an using Entity Framework 5.0 Database First approach.

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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