0

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?

2 Answers 2

1

You don't have to configure anything. Just gen the model from the db and then

 var query = context.Tasks.Include("Notes");
Sign up to request clarification or add additional context in comments.

2 Comments

I use it when I am creating new query? For example GetAllTasks()?
Yes, get all tasks and any notes associated
1

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

1 Comment

Thanks for your answer, Doesn't the Task class needs to have List of Notes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.