ASP.Net MVC Entity Framework Relationships

Crow

New Member
From what I understand I am doing this correctly. I have a blog table with a relationship of CategoryId on the table Category's id field. But I can not get it to resolve within my controller when I use the .include() command. Here is the code.Blog.cs\[code\]public class Blog{ [Key] public int Id { get; set; } [Required, StringLength(50)] public string Title { get; set; } [DataType(DataType.Date)] public DateTime PostedDate { get; set; } [Required] public string Meat { get; set; } [Required, StringLength(25)] public int CategoryId { get; set; } public string FriendlyUrl { get; set; } public virtual ICollection<Category> Category { get; set; }}public partial class BlogDbContext : DbContext{ public DbSet<Blog> Blogs { get; set; }}\[/code\]Category.cs\[code\]public class Category{ [Key] public int Id { get; set; } [Required, StringLength(50)] public string Title { get; set; } public string FriendlyUrl { get; set; }}public partial class BlogDbContext : DbContext{ public DbSet<Category> Categories { get; set; }}\[/code\]BlogController\[code\]public class BlogController : Controller{ private readonly BlogDbContext _db = new BlogDbContext(); public ActionResult Index() { var blogs = _db.Blogs.Include(c => c.Category); //This is where the error occurs return View(blogs.ToList()); }}\[/code\]
mCnyQ.jpg
 
Back
Top