Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include() in LINQ to Entities query

I have the following models in my ASP.NET MVC 3 project:

public class Task {     public int Id { get; set; }     public DateTime CreatedOn { get; set; }     public TaskStatus Status { get; set; } }  public class TaskStatus {     public int Id { get; set; }     public string Description { get; set; } } 

For reference, here is my DbContext class:

public class TaskManagerSets : DbContext {     public DbSet<Task> TaskSet { get; set; }     public DbSet<TaskStatus> TaskStatusSet { get; set; } }     

Then I have a List Action in my TaskController:

TaskManagerSets dbcontext = new TaskManagerSets(); public ActionResult List() {     var tasks = from tsk in dbcontext.TaskSet.Include("TaskStatusSet")                 select tsk;     return View(tasks.ToList()); } 

Finally I have the Task List View:

 @model IEnumerable<TaskManager.Models.Task>   <ul>  @foreach (var tsk in Model)   {      <li>@tsk.Id | @tsk.CreatedOn | @tsk.Status.Description</li>   }   </ul> 

When I execute my project I get the following error:

A specified Include path is not valid. The EntityType 'CodeFirstNamespace.Task' does not declare a navigation property with the name 'TaskStatus'.

The problem is definitely on the Include("TaskStatusSet") but how should I fix this?

like image 330
nunaxe Avatar asked Apr 13 '11 11:04

nunaxe


People also ask

Which is correct about LINQ to entities?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.

What is include in Entity Framework?

Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.

Can I use LINQ with Entity Framework?

Entity Framework Core uses Language-Integrated Query (LINQ) to query data from the database. LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries.


1 Answers

The navigation property name in your Task class is Status. So, you would have to use:

var tasks = from tsk in dbcontext.TaskSet.Include("Status")             select tsk; 

But since you are working with the DbContext API a better option is to use the type-safe overload of Include:

using System.Data.Entity; // You must add a using statement for this namespace to have the following  // lambda version of Include available  //...  var tasks = from tsk in dbcontext.TaskSet.Include(t => t.Status)             select tsk; 

You will get Intellisense and compile-time checks which helps to avoid issues with wrong strings like you had.

like image 103
Slauma Avatar answered Oct 05 '22 11:10

Slauma