C# Linq到SQL中的类型继承
我在SQLServer中有一些表,还有一组视图和函数,它们向这些表中添加了一些额外的列。C#Linq to SQL向导只需为我创建所有类型,我就可以使用它们 我的问题是: 添加的信息非常小,我希望视图和函数返回的对象的类型与主表相同,但自动生成的类型不同 我只需从表类型(比如C# Linq到SQL中的类型继承,c#,sql-server,linq-to-sql,C#,Sql Server,Linq To Sql,我在SQLServer中有一些表,还有一组视图和函数,它们向这些表中添加了一些额外的列。C#Linq to SQL向导只需为我创建所有类型,我就可以使用它们 我的问题是: 添加的信息非常小,我希望视图和函数返回的对象的类型与主表相同,但自动生成的类型不同 我只需从表类型(比如Table1)派生一个类型(比如View1),然后编写一个代码,用派生类型调用我的函数(或者打开我的视图)。但是,它抛出类型为invalidooperationexception的异常,并说类似于。。。这是继承的根吗? 所以
Table1
)派生一个类型(比如View1
),然后编写一个代码,用派生类型调用我的函数(或者打开我的视图)。但是,它抛出类型为invalidooperationexception
的异常,并说类似于。。。这是继承的根吗?
所以我想知道这在Linq到SQL和C#中是可能的,如果是,我怎么做
下面的示例需要引用:
- System.Data.Linq.Mapping
- System.Data.Linq
- 系统组件模型
- 系统反思
// Something like this added to the designer of dbml.
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Servers")]
public partial class DBServer : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Guid _ID;
private string _ServerName;
private string _ServerIPs;
private string _Name;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIDChanging(System.Guid value);
partial void OnIDChanged();
partial void OnServerNameChanging(string value);
partial void OnServerNameChanged();
partial void OnServerIPsChanging(string value);
partial void OnServerIPsChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
#endregion
public DBServer()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", DbType="UniqueIdentifier NOT NULL", IsPrimaryKey=true)]
public System.Guid ID
{
get
{
return this._ID;
}
set
{
if ((this._ID != value))
{
this.OnIDChanging(value);
this.SendPropertyChanging();
this._ID = value;
this.SendPropertyChanged("ID");
this.OnIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ServerName", DbType="NVarChar(128) NOT NULL", CanBeNull=false)]
public string ServerName
{
get
{
return this._ServerName;
}
set
{
if ((this._ServerName != value))
{
this.OnServerNameChanging(value);
this.SendPropertyChanging();
this._ServerName = value;
this.SendPropertyChanged("ServerName");
this.OnServerNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ServerIPs", DbType="NVarChar(255) NOT NULL", CanBeNull=false)]
public string ServerIPs
{
get
{
return this._ServerIPs;
}
set
{
if ((this._ServerIPs != value))
{
this.OnServerIPsChanging(value);
this.SendPropertyChanging();
this._ServerIPs = value;
this.SendPropertyChanged("ServerIPs");
this.OnServerIPsChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="NVarChar(255)")]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name != value))
{
this.OnNameChanging(value);
this.SendPropertyChanging();
this._Name = value;
this.SendPropertyChanged("Name");
this.OnNameChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我将其添加到一个单独的文件中:
public class DBServerEx: DBServer {
long? _AssignedPermissions;
long _InheritedPermissions;
long _ActivePermissions;
[ColumnAttribute(Storage="_AssignedPermissions", DbType="BigInt")]
public long? AssignedPermissions {
get {
return this._AssignedPermissions;
}
set {
if((this._AssignedPermissions!=value)) {
this._AssignedPermissions=value;
}
}
}
[ColumnAttribute(Storage="_InheritedPermissions", DbType="BigInt NOT NULL")]
public long InheritedPermissions {
get {
return this._InheritedPermissions;
}
set {
if((this._InheritedPermissions!=value)) {
this._InheritedPermissions=value;
}
}
}
[ColumnAttribute(Storage="_ActivePermissions", DbType="BigInt NOT NULL")]
public long ActivePermissions {
get {
return this._ActivePermissions;
}
set {
if((this._ActivePermissions!=value)) {
this._ActivePermissions=value;
}
}
}
}
在该文件中,我有:(注意,它是一个分部类,
MyDataContext
继承了DataContext
)
现在,当我打电话时
from s in Context.SelectServerWithPermissions(uID)
select s;
我得到了无效操作异常
来自
LINQ-to-SQL支持单表映射。换句话说,完整的继承层次结构存储在单个数据库表中。
更多链接:
您能提供一个代码示例吗,在您得到异常时。@NasmiSabeer我添加了一个sample@KenKin我更改了我正在阅读的部分,并删除了我以前的注释。LINQ到SQL主要是关于直接映射到数据库表和视图。如果你想让你的概念模型和物理模型不同,那么你应该使用实体框架。我已经看过那些页面了,他们说了一些关于一个表中的数据和各种结构的东西。例如,表中的教师和人(如您的示例所示)。但我的函数创建了一个新类型,正如您在我的示例中看到的,它将多个字段添加到原始表中。现在我该怎么办?
MyDataContext Context;
from s in Context.SelectServerWithPermissions(uID)
select s;