2013-01-12 2 views
0

SQL Server에 일부 테이블이 있고 이러한 테이블에 추가 열을 추가하는 뷰 및 함수 집합이 있습니다. C# Linq-to-SQL 마법사는 나를 위해 모든 유형을 작성하기 만하면됩니다.Linq-to-SQL의 상속 유형

내 문제는 : 추가 된 정보가 매우 작습니다 및보기 및 함수에서 반환 된 개체의 형식을 주 테이블과 동일하지만 자동 생성 된 형식이 다릅니다.

테이블 유형 (예 : Table1)에서 유형 (예 : View1)을 파생시키고 파생 형식으로 내 기능을 호출하거나 내보기를 열 수있는 코드를 작성합니다. 그러나 InvalidOperationException 유형을 예외로 지정하고 ... is this the root of inheritance?과 같이 말합니다.

Linq-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)); 
     } 
    } 
} 

내가 별도의 파일이 추가 :
가 (이 부분 클래스의 것을 참고 MyDataContextDataContext 상속) 다른 파일에서

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); 
    } 
} 

을 :

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 Context; 

지금,

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

를 호출 할 때 나는 InvalidOperationException를 얻을.

+1

예외가 발생하는 시점에서 코드 예제를 제공 할 수 있습니까? –

+0

@NasmiSabeer 샘플을 추가했습니다 – BigBoss

+0

@KenKin 그 부분을 변경합니다 – BigBoss

답변

0

Inheritance Support (LINQ to SQL)

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

더 많은 링크에서 :

How to: Map Inheritance Hierarchies (LINQ to SQL)

How to: Configure Inheritance by Using the O/R Designer

Linq to SQL Inheritance

LINQ to SQL Inheritance model in an ASP.Net application

+0

나는 이미 그 페이지들을 보았다, AFAIK 그들은 다양한 구조를 가진 하나의 테이블에 데이터에 관해 말하고있다. 예를 들어 교사와 사람의 테이블에있는 사람 (예를 들어). 내 함수에서 볼 수 있듯이 내 함수는 원래 테이블에 여러 필드를 추가합니다. 이제 어떻게 처리 할 수 ​​있을까요? – BigBoss