2010-11-14 58 views
0

我有幾個表具有相同的結構(例如隊列1,隊列2等)。出於行政原因,我必須將它們分開。某些隊列表可能由應用程序創建。如何管理多個表具有相同的結構

我正在使用數據上下文和LINQ和C#。

現在我想對所有表執行操作。我通過這種方式檢索我的桌子。

 IEnumerable<MetaTable> tables = appelsDataContext.Mapping.GetTables(); 
     foreach (MetaTable table in tables) 
     { 
     } 

簡而言之,我想更新所有這些表中的userID字段。我不知道如何做到這一點,因爲從我的dataContext的角度來看,即使表具有相同的結構,它們都是不同的實體。

非常感謝您提供的任何幫助。非常感謝! Mathieu

回答

1

如果您在解決方案資源管理器中的dbml文件下查看您的DataClasses1.designer.cs文件(或任何您命名它的文件),則會發現類似這樣的類。

[global::System.Data.Linq.Mapping.TableAttribute(Name="Production.Product")] 
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged 
{ 
//... 
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 
public int ProductID 
{ 
    get 
    { 
     return this._ProductID; 
    } 
    set 
    { 
     if ((this._ProductID != value)) 
     { 
      this.OnProductIDChanging(value); 
      this.SendPropertyChanging(); 
      this._ProductID = value; 
      this.SendPropertyChanged("ProductID"); 
      this.OnProductIDChanged(); 
     } 
    } 
} 
//... 
} 

如果您創建一個接口

public interface ITableHasProductId 
{ 
    int ProductID {get; set;} 
} 

您可以應用界面,所有的表,把下面的代碼在一個單獨的文件(以免丟失更改時重新生成數據上下文)

public partial class Product : ITableHasProductId {} 

然後你可以把你所有的表放在一個IEnumrable<ITableHasProductId>並更新產品ID。

+0

Awsome!非常感謝您的幫助! – Mathieu 2010-11-14 22:30:39

+0

雖然有一點細節。您的代碼在「public int ProductID {get; set;}」中提供了一個錯誤,指出「public」修飾符在那裏是錯誤的。但是,如果它不公開,我怎樣才能訪問ProductID表字段?再次感謝! – Mathieu 2010-11-15 02:38:25

+0

@Mathieu,我更新了我的答案,接口成員應該沒有公共修飾符,它們是隱式公開的。 – 2010-11-15 06:18:21

相關問題