简体   繁体   English

“System.Data.Linq.DataContext”类型在未引用的程序集中定义

[英]The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced

The Problem 问题

Error when going to a specific page (in local debug): CS0012: The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. 转到特定页面时出错(在本地调试中):CS0012:类型'System.Data.Linq.DataContext'在未引用的程序集中定义。 You must add a reference to assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 您必须添加对程序集'System.Data.Linq,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用。

Here are the referenced lines of code in the error message: 以下是错误消息中引用的代码行:

Line 28: 第28行:
Line 29: 第29行:
Line 30: public class _Page_Views_blah_granny_cshtml : System.Web.Mvc.WebViewPage { 第30行:公共类_Page_Views_blah_granny_cshtml:System.Web.Mvc.WebViewPage {
Line 31: 第31行:
Line 32: #line hidden 第32行:#line隐藏

All other pages work great - this only happens when accessing one page in particular. 所有其他页面都很有效 - 这只在特别是访问一个页面时才会发生。 The reference is working on all other pages. 该引用适用于所有其他页面。 As far as I am able to determine, this is not a problem with the reference. 据我所知,这不是引用的问题。

I've spent a good block of time researching this issue. 我花了很多时间来研究这个问题。

All the answers I found suggested going to web.config and putting an assembly reference to linq in system.web > configuration > assemblies. 我发现的所有答案建议转到web.config并在system.web> configuration> assemblies中将linq的汇编引用放入。 Mine doesn't have assemblies listed and I suspect this is more for older versions. 我没有列出组件,我怀疑这是旧版本的更多。 I did it anyway. 无论如何,我做到了。 It gave me another error saying it has no idea what to do with assemblies. 它给了我另一个错误,说它不知道如何处理程序集。

I deleted system.data.linq and added it in again. 我删除了system.data.linq并再次添加它。

I rebooted both VS and my computer. 我重新启动了VS和我的电脑。

My code - as generated by default by VS - has System.Linq. 我的代码 - 由VS默认生成 - 具有System.Linq。

Background - This is how this started: 背景 - 这是如何开始的:

The application is MVC 4, C#. 该应用程序是MVC 4,C#。

I created a new class in my DataContext, added a new controller, and created a strongly typed view. 我在DataContext中创建了一个新类,添加了一个新的控制器,并创建了一个强类型视图。

Here is some very similar code (likely not needed, but just in case). 这是一些非常相似的代码(可能不需要,但以防万一)。

Two classes: 两个班:

public class granny { 
            public string data { get; set; }
            public string criteria { get; set; }
}

public List<granny> getGranny() {
    var a = from x in grannytable
            join dx in anothertable
            on x.criteria equals dx.criteria
            select new granny {
                data = x.somenewdata;
            }; 
    return a.ToList();
}

Here is the controller: 这是控制器:

    public ActionResult granny() {
        return View(db.getGranny());
    }

Nothing fancy there. 没有什么花哨的。

The page is a typical razor view, strongly typed "List"...it has a table that it iterates to create, dumping the data as it goes. 该页面是一个典型的剃刀视图,强类型“列表”...它有一个表,它迭代创建,随时转储数据。

I am happy to provide any additional code as needed. 我很乐意根据需要提供任何其他代码。

I have not messed with web.config. 我没有搞乱web.config。 I have not removed or tweaked any of the references on the pages. 我没有删除或调整页面上的任何引用。 All of the other views work marvelously. 所有其他观点都非常有效。

When I run it locally, and attempt to go to /granny I get the above error. 当我在本地运行它,并尝试去/ granny我得到上述错误。

Thanks for your help! 谢谢你的帮助!

THE SOLUTION : I went into references, and for the System.Linq (or for older versions, I suppose, System.Data.Linq) and changed CopyLocal to True. 解决方案 :我进入了引用,对于System.Linq(或者对于旧版本,我认为是System.Data.Linq)并将CopyLocal更改为True。

This worked for me: 这对我有用:

  • Navigate to the web project's References node 导航到Web项目的References节点
  • Find the reference to System.Data.Linq 找到对System.Data.Linq的引用
  • Open the VS Properties Window 打开VS属性窗口
  • In the properties window, change Copy Local: False to True 在属性窗口中,将Copy Local:False更改为True

Before this, the System.Data.Linq.dll was not being copied into the bin directory. 在此之前,System.Data.Linq.dll没有被复制到bin目录中。 (Copying it manually into the bin directory also resolved the error) (手动将其复制到bin目录中也解决了错误)

MVC cannot compile your view on the fly. MVC无法动态编译您的视图。 It looks like your view contains a reference to a DataContext. 看起来您的视图包含对DataContext的引用。 Adding a reference in web.config should tell the compiler to look for the file: 在web.config中添加引用应告诉编译器查找该文件:

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

Note: the assembly should be added under the compilation element. 注意:程序集应添加在编译元素下。

Can you share your view, controller and datacontext code? 你能分享你的视图,控制器和datacontext代码吗?

尝试在问题视图的顶部添加@using System.Data.Linq ,并在Views文件夹<add namespace="System.Data.Linq" />到web.config的<pages>部分。

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

相关问题 LINQ to SQL System.Data.Linq.DataContext未引用 - LINQ to SQL System.Data.Linq.DataContext not referenced 静态System.Data.Linq.DataContext的库 - Library for static System.Data.Linq.DataContext 扩展 System.Data.Linq.DataContext - Extending System.Data.Linq.DataContext 在EntityFramework 6中的System.Data.Linq.DataContext中的日志执行时间 - Log Execution Time in System.Data.Linq.DataContext like in EntityFramework 6 类型“ System.Data.Linq.Binary”在未引用的程序集中定义 - Type 'System.Data.Linq.Binary' is defined in an assembly that is not referenced 没有从&#39;ApplicationDbContext&#39;到&#39;System.Data.Linq.DataContext&#39;的隐式引用转换。 - There is no implicit reference conversion from 'ApplicationDbContext' to 'System.Data.Linq.DataContext' 类型&#39;System.Linq.IQueryable`1 <T0> &#39;在未引用的程序集中定义 - The type 'System.Linq.IQueryable`1<T0>' is defined in an assembly that is not referenced 通过System.Data.Linq.DataContext插入SQL Server时出错 - Error when inserting in to SQL Server through System.Data.Linq.DataContext 什么更好:重用 System.Data.Linq.DataContext 上下文或尽快处理它? - What is better: reusing System.Data.Linq.DataContext context or disposing of it ASAP? “System.IDisposable”类型在未引用的程序集中定义 - The type 'System.IDisposable' is defined in an assembly that is not referenced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM