开发者

CS0012: The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced

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.DataBi开发者_StackOverflow社区nd();
    }
}

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?


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.


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.


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;


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.


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


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;


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜