简体   繁体   English

找不到DataClassesDataContext-VS2012

[英]DataClassesDataContext Not Found - VS2012

I'm doing a project in ASP.NET and I'm using Visual Studio 2012 with .NET 4.5. 我正在ASP.NET中做一个项目,并且正在将Visual Studio 2012与.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. 我正在一个项目上,并从一个空的Web应用程序开始。 I've added a folder "App_Code" in which I put my Models and DataClasses. 我添加了一个文件夹“ App_Code”,其中放入了我的模型和数据类。 I've added a LINQ-to-SQL class with the correct tables and saved this file in the App_Code folder. 我添加了具有正确表的LINQ-to-SQL类,并将此文件保存在App_Code文件夹中。

Now, when I add a Class to the App_Code folder, the class can't find the DataClasses. 现在,当我将一个类添加到App_Code文件夹时,该类找不到DataClasses。

This is the code of my class "Product" in the folder App_Code: 这是文件夹App_Code中我的类“ Product”的代码:

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. Visual Studio不允许我运行代码。 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". Product.cs文件的生成操作为“编译”。

Can anyone help or give advice about this? 有人可以帮忙吗? Or try to help get the DataClasses recognised? 还是尝试帮助识别数据类? I'm still learning ASP.NET. 我仍在学习ASP.NET。 If you need other data or other code, just tell me. 如果您需要其他数据或其他代码,请告诉我。

Thanks in advance! 提前致谢!

I've removed everything from the folder App_Code and made a new folder called Models. 我已经从文件夹App_Code中删除了所有内容,并创建了一个名为Models的新文件夹。 Placed everything (class Product and a new OutsideDataClass) in the folder Models and suddenly it works. 将所有内容(Product类和新的OutsideDataClass类)放置在Models文件夹中,然后突然起作用。 Weird. 奇怪的。

App_Code seems like a bad idea. App_Code似乎是个坏主意。

Firstly, make sure you have Linq to SQL added manually from the Visual Studio installer. 首先,请确保已从Visual Studio安装程序手动添加了Linq to SQL。 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; 错误正在寻找使您连接到的数据库具有通用版本的Linq dbml文件,然后在解决方案/项目上单击鼠标右键以添加该dbml。

Choose Add New Item, then under Data, choose Linq-To-SQL Classes . 选择添加新项,然后在数据下,选择Linq-To-SQL类。 This will create an empty DBML file. 这将创建一个空的DBML文件。 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. 然后,您需要创建与数据库的服务器连接(如果尚未建立),并将所需的表拖到DBML设计器中。

Looks like your OutsideDataClassesDataContext() is in another namespace or in another project of your solution. 看起来您的OutsideDataClassesDataContext()位于解决方案的另一个名称空间或另一个项目中。

Try to shorten your namespace name to Outside or add reference from project, where your class exists. 尝试将名称空间名称缩短为Outside或从类所在的项目中添加引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM