如何管理具有相同结构的多个表

时间:2010-11-14 21:31:02

标签: c# visual-studio linq linq-to-sql

我有几个具有相同结构的表(例如Queue1,Queue2等)。出于管理原因,我必须将它们分开。某些队列表可能由应用程序创建。

我正在使用数据上下文和linq和C#。

现在我想对所有表执行操作。我以这种方式回顾我的桌子。

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

简单地说,我想更新所有这些表中的userID字段。我不知道如何做到这一点,因为从我的dataContext的角度来看,即使表具有相同的结构,它们都是不同的实体。

非常感谢您提供的任何帮助。非常感谢! 马修

1 个答案:

答案 0 :(得分: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>中并更新ProductID。

相关问题