Kez*_*zer 13 c# linq asp.net-mvc linq-to-sql
我有一个反映我的dbml文件的类,它扩展了DataContext,但由于一些奇怪的原因,它告诉我
System.Data.Linq.DataContext'不包含带'0'参数的构造函数"
我已经遵循了这方面的各种教程,并没有遇到这个问题,VS似乎无法修复它.
这是我的实施
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Text;
using IntranetMvcAreas.Areas.Accounts.Models;
namespace IntranetMvcAreas
{
  partial class ContractsControlDataContext : DataContext
  {
    [FunctionAttribute(Name="dbo.procCC_Contract_Select")]
    [ResultType(typeof(Contract))]
    [ResultType(typeof(ContractCostCentre))]
    [ResultType(typeof(tblCC_Contract_Data_Terminal))]
    [ResultType(typeof(tblCC_CDT_Data_Service))]
    [ResultType(typeof(tblCC_Data_Service))]
    public IMultipleResults procCC_Contract_Select(
        [Parameter(Name = "ContractID", DbType = "Int")] System.Nullable<int> ContractID,
        [Parameter(Name = "ResponsibilityKey", DbType = "Int")] System.Nullable<int> ResponsibilityKey,
        [Parameter(Name = "ExpenseType", DbType = "Char")] System.Nullable<char> ExpenseType,
        [Parameter(Name = "SupplierID", DbType = "Int")] System.Nullable<int> SupplierID)
    {
      IExecuteResult result = this.ExecuteMethodCall(this, (MethodInfo)(MethodInfo.GetCurrentMethod()), ContractID, ResponsibilityKey, ExpenseType, SupplierID);
      return (IMultipleResults)result.ReturnValue;
    }
  }
}
这就是ContractsControlDataContext问题所在
(顺便说一下,这跟我最近发的一篇文章没关系,只是我在做同样的事情)
编辑
这可能值得澄清,所以请仔细阅读.
如果不在分部类中扩展DataContext,则ExecuteMethodCall无法访问.
"Intranet.ContractsControlDataContext"不包含关于"ExecuteMethodCall"和没有扩展方法的定义"ExecuteMethodCall"接受类型"Intranet.ContractsControlDataContext"的第一个参数可以找到(是否缺少using指令或程序集引用?)
也许我错过了一些非常愚蠢的东西?
解决了
我想也许Visual Studio在这里挣扎,但我完全依赖于自动生成的代码.右键单击数据库建模语言设计视图并点击"查看代码"时,它会在特定命名空间中自动为您创建一个部分类,但是,此命名空间是错误的.如果有人能为我澄清这一点,我将非常感激.
.designer.cs文件位于namespace Intranet.Areas.Accounts.Models,但.cs文件(由 Visual Studio 为 .designer.cs文件生成的部分类)位于.很容易找到比我更有经验的人.namespace Intranet
现在真正的问题是,我的答案是否正确?因为你们中许多人为找到这个问题做出了贡献
linq的对象DataContext没有空构造函数.由于它没有空构造函数,因此必须传递除基础之外的其中一个项目.
从DataContext的MetaData.
// Summary:
//     Initializes a new instance of the System.Data.Linq.DataContext class by referencing
//     the connection used by the .NET Framework.
//
// Parameters:
//   connection:
//     The connection used by the .NET Framework.
public DataContext(IDbConnection connection);
//
// Summary:
//     Initializes a new instance of the System.Data.Linq.DataContext class by referencing
//     a file source.
//
// Parameters:
//   fileOrServerOrConnection:
//     This argument can be any one of the following: The name of a file where a
//     SQL Server Express database resides.  The name of a server where a database
//     is present. In this case the provider uses the default database for a user.
//      A complete connection string. LINQ to SQL just passes the string to the
//     provider without modification.
public DataContext(string fileOrServerOrConnection);
//
// Summary:
//     Initializes a new instance of the System.Data.Linq.DataContext class by referencing
//     a connection and a mapping source.
//
// Parameters:
//   connection:
//     The connection used by the .NET Framework.
//
//   mapping:
//     The System.Data.Linq.Mapping.MappingSource.
public DataContext(IDbConnection connection, MappingSource mapping);
//
// Summary:
//     Initializes a new instance of the System.Data.Linq.DataContext class by referencing
//     a file source and a mapping source.
//
// Parameters:
//   fileOrServerOrConnection:
//     This argument can be any one of the following: The name of a file where a
//     SQL Server Express database resides.  The name of a server where a database
//     is present. In this case the provider uses the default database for a user.
//      A complete connection string. LINQ to SQL just passes the string to the
//     provider without modification.
//
//   mapping:
//     The System.Data.Linq.Mapping.MappingSource.
public DataContext(string fileOrServerOrConnection, MappingSource mapping);
像这样简单的东西可行.从DataConext继承的任何类必须至少将其中一种类型传递给基础构造函数.
public class SomeClass : System.Data.Linq.DataContext
{
    public SomeClass(string connectionString)
        :base(connectionString)
    {
    }
}
我假设名称空间和(数据上下文)类型名称是正确的...首先仔细检查.
这听起来好像在代码生成失败了,所以你只需要您的数据上下文(而不是一半的IDE是为了提供)的一半.LINQ-to-SQL中存在一个已知错误,如果(如在您的情况下)using声明位于命名空间之上,则可能会失败.不,我不是在开玩笑.尝试更改代码:
namespace IntranetMvcAreas
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Reflection;
    using System.Text;
    using IntranetMvcAreas.Areas.Accounts.Models;
    // the rest of your code
现在进入设计器,调整一些东西(例如,更改属性的名称并再次更改它)并点击保存(这会强制执行codegen).现在看看它是否有效.