aspr
aspr

Reputation: 11

DataClassesDataContext Not Found - VS2012

I'm doing a project in ASP.NET and I'm using Visual Studio 2012 with .NET 4.5. I've been searching online for my problem but haven't found any solution yet. Maybe anyone here can help me.

I'm working on a project and started with an empty Web Application. I've added a folder "App_Code" in which I put my Models and DataClasses. I've added a LINQ-to-SQL class with the correct tables and saved this file in the App_Code folder.

Now, when I add a Class to the App_Code folder, the class can't find the DataClasses.

This is the code of my class "Product" in the folder App_Code:

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

namespace Outside.App_Code
{
    public partial class Product
    {
        private static OutsideDataClassesDataContext db;

        public static List<Product> GetAll(int id)
        {
            // Init the DB
            db = new OutsideDataClassesDataContext();
            // Get all the products and return
            return db.Products.Where(a => a.id > 0).ToList();
        }
    }
}

Visual Studio won't let me run the code. I get this error:

The type or namespace name 'OutsideDataClassesDataContext' could not be found (are you missing a using directive or an assembly reference?) \App_Code\Product.cs    10  24  Outside

The build action of the file Product.cs is "Compile".

Can anyone help or give advice about this? Or try to help get the DataClasses recognised? I'm still learning ASP.NET. If you need other data or other code, just tell me.

Thanks in advance!

Upvotes: 0

Views: 2619

Answers (3)

Liza Boshoff
Liza Boshoff

Reputation: 11

Firstly, make sure you have Linq to SQL added manually from the Visual Studio installer. It will be under 'Individual Components'

The error is looking for the Linq dbml file that makes a generic version of the database you are connected to, to add this dbml right-click on your solution/project;

Choose Add New Item, then under Data, choose Linq-To-SQL Classes . This will create an empty DBML file. You'll then need to create a server connection to a database (if you have not already) and drag the tables you want into the DBML designer.

Upvotes: 1

aspr
aspr

Reputation: 11

I've removed everything from the folder App_Code and made a new folder called Models. Placed everything (class Product and a new OutsideDataClass) in the folder Models and suddenly it works. Weird.

App_Code seems like a bad idea.

Upvotes: 1

rock_walker
rock_walker

Reputation: 473

Looks like your OutsideDataClassesDataContext() is in another namespace or in another project of your solution.

Try to shorten your namespace name to Outside or add reference from project, where your class exists.

Upvotes: 0

Related Questions