0

我在 SQL Server 中有一些表和一组视图和函数,它们为这些表添加了一些额外的列。C# Linq-to-SQL 向导只需为我创建所有类型并且我可以使用它们。

我的问题是:添加的信息非常小,我希望视图和函数返回的对象类型与主表相同,但自动生成的类型不同。

我只是View1从表类型 (say ) 中派生一个类型 (say Table1) 并编写代码以使用派生类型调用我的函数 (或打开我的视图)。但是,它会引发类型异常InvalidOperationException并说出类似... is this the root of inheritance?.

所以我想知道这在 Linq-to-SQL 和 C# 中是否可行,如果可以,我该怎么办?

以下示例需要参考:

  • System.Data.Linq.Mapping
  • System.Data.Linq
  • System.ComponentModel
  • 系统反射

例子

// 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

partial class MyDataContext {
    [FunctionAttribute(Name="dbo.uf_SelectServerWithPermissions", IsComposable=true)]
    public IQueryable<DBServerEx> SelectServerWithPermissions([ParameterAttribute(Name="UID", DbType="BigInt")] long uID) {
        return this.CreateMethodCallQuery<DBServerEx>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), uID);
    }
}

在另一个文件中:

MyDataContext Context;

现在,当我打电话时

from s in Context.SelectServerWithPermissions(uID)
select s;

我得到InvalidOperationException.

4

1 回答 1

0

继承支持(LINQ to SQL)

LINQ to SQL supports single-table mapping. In other words, a complete inheritance hierarchy is stored in a single database table.

更多链接:

如何:映射继承层次结构 (LINQ to SQL)

如何:使用 O/R 设计器配置继承

Linq 到 SQL 的继承

ASP.Net 应用程序中的 LINQ to SQL 继承模型

于 2013-01-12T14:29:31.693 回答