Mathieu
Mathieu

Reputation: 4520

How to manage multiple tables with the same structure

I have few tables that have the same structure (e.g. Queue1, Queue2, etc.). I have to keep them separate for administrative reasons. Some Queue tables may be created by the application.

I am using a data context and linq and C#.

Now I want to perform an operation on all the tables. I retreive my tables this way.

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

Simply put, I want to update a userID field in all these tables. I have no idea how to do this because from my dataContext point of view, even though the tables have the same structure, they are all different entities.

Thank you very much for any help you can provide. It is appreciated! Mathieu

Upvotes: 0

Views: 388

Answers (1)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47058

If you look in your DataClasses1.designer.cs-file (or whatever you have named it) under the dbml-file in solution explorer you will find your classes like this.

[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();
        }
    }
}
//...
}

etc.

If you create an interface

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

you can apply that interface to all your tables, put the following code in a separate file (to avoid loosing your changes when regenerating the data context)

public partial class Product : ITableHasProductId {}

Then you can put all your tables in an IEnumrable<ITableHasProductId> and update the ProductID.

Upvotes: 1

Related Questions