代码之家  ›  专栏  ›  技术社区  ›  Kieran Senior

扩展System.Data.Linq.DataContext

  •  12
  • Kieran Senior  · 技术社区  · 16 年前

    我有一个类反映了我的DBML文件,它扩展了数据上下文,但出于某种奇怪的原因,它告诉我

    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文件(生成了部分类 对于 .designer.cs文件 通过 Visual Studio)在 namespace Intranet . 很容易找到比我更有经验的人。

    现在真正的问题是,谁的答案是正确的?因为你们中的许多人都为发现这个问题做出了贡献。

    5 回复  |  直到 16 年前
        1
  •  6
  •   David Basarab    16 年前

    Linq的对象DataContext没有空构造函数。因为它没有空的构造函数,所以必须将它除外的一个项传递给基。

    来自DataContext的元数据。

    // 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)
        {
    
        }
    }
    
        2
  •  5
  •   Marc Gravell    16 年前

    我是 假设 命名空间和(数据上下文)类型名称是否正确…先仔细检查一下。

    在我看来,代码生成失败了,所以你只有 你的 一半的数据上下文(而不是IDE要提供的那一半)。在LinqToSQL中有一个已知的bug,如果(如您的情况) 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)。现在看看是否有效。

        3
  •  2
  •   mvr    16 年前

    大卫·巴沙伯的回答是正确的,应该被标记为答案。

    您的类没有提供任何构造函数,因此提供了默认的构造函数。只有在基类具有无参数构造函数时,才能提供派生类的默认构造函数。但是,本例中作为基类的DataContext类不提供无参数构造函数。这解释了编译器返回给您的错误消息。

    编辑 :

    例子:

    class A {
        public A(string s) {
        }
    }
    
    class B : A {
    }
    

    尝试编译,返回类B中的错误:

    “a”不包含采用“0”参数的构造函数

        4
  •  1
  •   cdonner    16 年前

    生成器关于构造函数的行为在某种程度上由DBML的连接属性控制。如果应用程序设置为true,并且有一个设置属性名,它将生成一个从程序集的应用程序设置中读取连接字符串的构造函数。 如果有连接字符串,它将在.designer.cs文件中生成一个带有硬编码连接字符串的构造函数。 如果两者都不存在,它将不会生成没有连接字符串参数的构造函数,并且您可以安全地在分部类中提供无参数的构造函数,而不会导致冲突。

    这些设置更改无法在数据库模式的往返过程中存活下来,但我只需在进行更改之后和保存DBML之前从属性中清除连接设置。

        5
  •  0
  •   Kieran Senior    16 年前

    @桑德:我觉得你走对了。而不是使用分部类并实现存储过程的函数 this blog 并使用了*.designer.cs文件来实现它。虽然我仍然遇到了无效类型转换的问题,但它确实解决了原来的问题。