6

Helllo, I get this error:

CS0246: The type or namespace name 'DataClasses1DataContext' could not be found (are you missing a using directive or an assembly reference?)

For this .aspx file:

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

public partial class WebApplication1_admin_Places : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        var query = (from m in db.Places orderby m.Name select m);
        PlacesList.DataSource = query;
        PlacesList.DataBind();
    }
}

The thing is, on / folder I can access DB, but on /admin folder i get this error.

What am I doing wrong?

EDIT

CS0012: The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

What does that mean?


  • see my comment for resolving that latest error. - RPM1984

8 답변


5

A couple of places to start:

  • Where is the dll that contains DataClasses1DataContext? Is there a reason that you can get to it from one folder and not the other
  • Is there a config file in the admin folder that is overriding values set in the root config file?

Edit

It looks like this is a configuration problem. The config probably says that the msl (model) file is in the current directory, it is in the root directory. Therefore it works when you are on the root but not when you are in admin.

see: MetadataException when using Entity Framework Entity Connection for a similar problem.


  • It's a compiler error he's getting, not a runtime one. Which means it has nothing to do with relative/absolute paths or configuration (these cause runtime errors). I think he just needs to reference the assembly correctly. - RPM1984
  • DataClasses1.dbml is located on \ but the .aspx trying to use it is located on \adamin\. how could this be resolved? - natiz
  • Okay, There is no Web.config file in admin folder, should there be? - natiz

5

This error can occur when an attempt to load an assembly is caused by the .ASPX markup, even if the assembly is already referenced by the project! The project-wide solution mentioned in passing here is to add the assembly to the list of assemblies in the web.config assemblies Element for compilation, eg.

<compilation>
 <assemblies>
  <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  ...

The alternative on a per-page basis is the @ Assembly page attribute, but this has to be added to every page causing the ASP.NET runtime to attempt to load the missing assembly type.


1

What do you mean "on / folder I can access DB, but on /admin folder i get this error"?

It makes no difference which folder your in - the context uses the connection string in the configuration file (e.g web.config). Relative/absolute paths don't apply here, this is code, not a resource.

Do you have your data context in a seperate assembly to your web application?

You need to import the namespace, as with everything else:

E.g

using YourApplication.Data;


  • I solved that one, i forgot to include the namespace WebApplication1 {..... ------ but now a new thing came up: CS0012: The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. - natiz
  • Okay, there is no Web.Config on my admin folder, should there be? - natiz
  • @NZIM - that error means you need to reference System.Data.Linq in your web application. Add -> Reference -> System.Data.Linq. This is because your performing L2SQL operations in your UI. If you encapsulated your L2SQL code behind a repository, you would not need to add this reference. However, add the reference to System.Data.Linq for now. - RPM1984
  • As for the web.config - put the connection string in the "main" web.config (root). The areas web.config inherits from the main web.config, so should not ovverride the db connection (this is global to all areas). The only thing that should be in the areas web.config (if at all), are things like namespace registrations. - RPM1984
  • I'v added System.Data.Linq but now what i get is: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) - natiz

1

Just had this same error message pop up on me. Resolved by adding a reference in the project to System.Data.Linq through the VS 2010 references dialog.


  • I also solved it by doing this, the only problem is that I don't really know why it was needed in the first place. - kad81

0

Add Reference to your model. in C#

using ProjectName.Models;

Then Add following line in Application_Start() routine Database.SetInitializer<MyMVCTestContext>(null); (here MyMvcTestContext will be your data context name)

Check following list to make sure you are not missing any references. You may need all of them or may not.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.ModelBinding;
using System.Data;
using System.Data.Entity;
using MyMVCTest.Models;


0

If this project is LinqToSql Project and if data context is "DataClasses1DataContext" the dbml file name should be "DataClasses1.dbml"

If .dbml file id "MyLinqToSql.dbml" the datacontext name should be MyLinqToSqlDataContext.

Please check the .dbml file name and match the databcontext name as explained above.

Also if you start typing you would get the name automatically in IntelliSense.


0

So the fix for me was to add the LINQTOSQL To the folder where i user it + Add This Inside in the webConfig file :

<system.web>
<compilation debug="false" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  </assemblies>
</compilation>


0

Navigate to the web project's References node Find the reference to System.Data.Linq Open the VS Properties Window In the properties window, change Copy Local: False to True Before this, the System.Data.Linq.dll was not being copied into the bin directory. (Copying it manually into the bin directory also resolved the error)

From: https://stackoverflow.com/a/34356930/125938

Linked


Related

Latest