1

Closed

How to check table have relationship many to many ?

description

DatabaseSchema ds;
        string tableName;
        string _friendlyName;
        const string providername = "System.Data.SqlClient";
        const string connectionString =
            @"Data Source=MINHIT\MISASME2012;Initial Catalog=lmaccounting;user id=sa;password=123";
        var dbReader = new DatabaseReader(connectionString, providername);
        ds = dbReader.ReadAll();
        foreach (var d in ds.Tables)
        {
            //Check N-N
            if (d.IsManyToManyTable())-->Don't find
            {
                Console.WriteLine("Table N-N: " + d.Name);
            }
        }
Closed Nov 21, 2015 at 3:15 PM by Martinjw

comments

Martinjw wrote Mar 9, 2015 at 6:47 AM

IsManyToManyTable() is looking for a specific pattern- the classic junction table/association table, where the primary keys are also foreign keys.

For instance, in standard Northwind, the Employees table and the Territories table have a junction table called EmployeeTerritories, which has a composite primary key comprising EmployeeId (pk + fk to employees) and TerritoryId (pk + fk to territories).

It is possible to implement many to many differently. You could have EmployeeTerritories with it's own single primary key (EmployeeTerritoriesId), and the other two columns are just foreign keys. This wouldn't be a classic junction table, so IsManyToManyTable() won't detect it. I wonder if that's why it's not fired for you??