这是我的 DBML 设计器代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public partial class DataClassesDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public DataClassesDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
}
这是我的 C# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdLogin_Click(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
}
}
当我编写 DataClassesDataContex db=new DataClassesDataContex(); 时它将显示 DataClassesDataContex 不包含采用 0 个参数的构造函数?
您的类中没有定义无参数构造函数
DataClassesDataContext
。默认情况下,应该生成另一个构造函数,它从应用程序设置中获取连接字符串:
public DataClassesDataContext() :
base(global::Foo.Properties.Settings.Default.BarConnectionString,
mappingSource)
{
OnCreated();
}
但由于某种原因它现在丢失了。你可以:
DataClassesDataContext
并手动添加无参构造函数步骤:
我通过以下步骤修复了它:
删除:
<connectionString>
<add name="exampleDB" connectionString="Data Source=example;Initial
Catalog=example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
删除:
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=D...8"/>
</assemblies>
您可以在
<assemblies>
中找到
<compilation></compilation>
如果您有多个
<add assembly />
标签,则您与数据库有多个连接。
添加另一个“example.dbml”再次添加您的表。
创建一个新的数据上下文对象。
问题是属性窗口中的连接被删除了。
查看这些属性
在之前附加的图像中可以看到 Connection 属性已被删除。 如果您无法读取连接,您可以将源代码控制恢复到之前的历史记录点,然后重新添加自上次提交以来的表和设置。
问题已解决!
.designer.cs 中的默认构造函数已恢复。
public DataClassesHelpTicketsDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["Help_TicketsConnectionString1"].ConnectionString, mappingSource)
{
OnCreated();
}