C# 确定Linq.表的T<;T>;运行时
我有两个结构相同的表,它们存在于两个独立的数据库中。两个数据库对象都继承DataContext类并共享一个公共接口,数据库中的每个表也是如此。问题是Linq.Table类需要一个类作为T,但我需要它作为一个接口,以便在运行时确定正确的表。下面是一些代码,我希望能解释这个问题 两个数据库对象共享的公共接口C# 确定Linq.表的T<;T>;运行时,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有两个结构相同的表,它们存在于两个独立的数据库中。两个数据库对象都继承DataContext类并共享一个公共接口,数据库中的每个表也是如此。问题是Linq.Table类需要一个类作为T,但我需要它作为一个接口,以便在运行时确定正确的表。下面是一些代码,我希望能解释这个问题 两个数据库对象共享的公共接口 interface ICommonDataContext { // Table <T> where T is a class Table<ITable1>
interface ICommonDataContext
{
// Table <T> where T is a class
Table<ITable1> Table1 { get; }
}
带有接口实现的自动生成Region1DataContext代码
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDB")]
public partial class Region1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public Region1DataContext() :
base(ConnectionString, mappingSource)
{
OnCreated();
}
public Region1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<ITable1> Table1
{
get
{
return this.GetTable<ITable1>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="Table1")]
public partial class Table1
{
private int _Col1;
private int _Col2;
public Table1()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col1", DbType="Int")]
public int Col1
{
get
{
return this._Col1;
}
set
{
if ((this._Col1 != value))
{
this._Col1 = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col2", DbType="Int")]
public int Col2
{
get
{
return this._Col2;
}
set
{
if ((this._Col2 != value))
{
this._Col2 = value;
}
}
}
}
{
public partial class Region1DataContext : ICommonDataContext { }
public partial class Table1 : ITable1 { }
}
其他分部类声明,包括接口实现
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDB")]
public partial class Region1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public Region1DataContext() :
base(ConnectionString, mappingSource)
{
OnCreated();
}
public Region1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public Region1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<ITable1> Table1
{
get
{
return this.GetTable<ITable1>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="Table1")]
public partial class Table1
{
private int _Col1;
private int _Col2;
public Table1()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col1", DbType="Int")]
public int Col1
{
get
{
return this._Col1;
}
set
{
if ((this._Col1 != value))
{
this._Col1 = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col2", DbType="Int")]
public int Col2
{
get
{
return this._Col2;
}
set
{
if ((this._Col2 != value))
{
this._Col2 = value;
}
}
}
}
{
public partial class Region1DataContext : ICommonDataContext { }
public partial class Table1 : ITable1 { }
}
我认为这个答案将为解决您的问题提供一些指导——为什么不添加一个参数来指示数据库呢?由于连接字符串不同,您已经知道连接了哪个数据库。因此,只需将数据库名称传递给例程,而不是动态执行。您还可以从connection属性获取数据库名称。在何处添加参数?