SQL Server にいくつかのテーブルがあり、それらのテーブルに列を追加する一連のビューと関数があります。C# Linq-to-SQL ウィザードは、すべての型を作成するだけで、それらを使用できます。
私の問題は次のとおりです。追加された情報は非常に小さく、ビューと関数から返されるオブジェクトのタイプをメインテーブルと同じにしたいのですが、自動生成されたタイプは異なります。
View1
テーブル型 ( など) から型 (などTable1
) を派生させ、派生型で関数を呼び出す (またはビューを開く) コードを記述します。ただし、タイプの例外をスローし、のInvalidOperationException
ようなことを言います... is this the root of inheritance?
。
したがって、Linq-to-SQL と C# でこれが可能かどうかを知りたいのですが、可能であればどうすればよいですか??
次の例では、次の参照が必要です。
- System.Data.Linq.Mapping
- System.Data.Linq
- System.ComponentModel
- System.Reflection
例
// 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
ます。