Creating LINQ to SQL Data Models' Data Contexts with ASP.NET MVC

Posted by Maxim Z. on Stack Overflow See other posts from Stack Overflow or by Maxim Z.
Published on 2010-04-02T19:09:32Z Indexed on 2010/04/02 19:13 UTC
Read the original article Hit count: 343

I'm just getting started with ASP.NET MVC, mostly by reading ScottGu's tutorial. To create my database connections, I followed the steps he outlined, which were to create a LINQ-to-SQL dbml model, add in the database tables through the Server Explorer, and finally to create a DataContext class.

That last part is the part I'm stuck on. In this class, I'm trying to create methods that work around the exposed data. Following the example in the tutorial, I created this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MySite.Models
{
    public partial class MyDataContext
    {
        public List<Post> GetPosts()
        {
            return Posts.ToList();
        }
        public Post GetPostById(int id)
        {
            return Posts.Single(p => p.ID == id);
        }

    }
}

As you can see, I'm trying to use my Post data table. However, it doesn't recognize the "Posts" part of my code. What am I doing wrong? I have a feeling that my problem is related to my not adding the data tables correctly, but I'm not sure.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc