Skip to content

Commit

Permalink
fix #593
Browse files Browse the repository at this point in the history
SchemaProvider.GetForeignKeys for Access and SqlCe
  • Loading branch information
ili committed Mar 12, 2017
1 parent 296ff9a commit 88c0e54
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
17 changes: 16 additions & 1 deletion Source/DataProvider/Access/AccessSchemaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace LinqToDB.DataProvider.Access
using Common;
using Data;
using SchemaProvider;
using System.Data.OleDb;

class AccessSchemaProvider : SchemaProviderBase
{
Expand Down Expand Up @@ -137,7 +138,21 @@ join dt in DataTypes on c.Field<int>("DATA_TYPE") equals dt.ProviderDbType

protected override List<ForeingKeyInfo> GetForeignKeys(DataConnection dataConnection)
{
return new List<ForeingKeyInfo>();
var data = ((OleDbConnection)dataConnection.Connection)
.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, new object[] { null, null });

var q = from fk in data.AsEnumerable()
select new ForeingKeyInfo
{
Name = fk.Field<string>("FK_NAME"),
ThisColumn = fk.Field<string>("FK_COLUMN_NAME"),
OtherColumn = fk.Field<string>("PK_COLUMN_NAME"),
ThisTableID = fk.Field<string>("FK_TABLE_CATALOG") + "." + fk.Field<string>("FK_TABLE_SCHEMA") + "." + fk.Field<string>("FK_TABLE_NAME"),
OtherTableID = fk.Field<string>("PK_TABLE_CATALOG") + "." + fk.Field<string>("PK_TABLE_SCHEMA") + "." + fk.Field<string>("PK_TABLE_NAME"),
Ordinal = ConvertTo<int>.From(fk.Field<long>("ORDINAL")),
};

return q.ToList();
}

protected override string GetProviderSpecificTypeNamespace()
Expand Down
16 changes: 13 additions & 3 deletions Source/DataProvider/SqlCe/SqlCeSchemaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,19 @@ from c in cs.AsEnumerable()

protected override List<ForeingKeyInfo> GetForeignKeys(DataConnection dataConnection)
{
//var fks = ((DbConnection)dataConnection.Connection).GetSchema("ForeignKeys");

return new List<ForeingKeyInfo>();
var data = dataConnection.Query<ForeingKeyInfo>(
@"SELECT
COALESCE(rc.CONSTRAINT_CATALOG, '') + '.' + COALESCE(rc.CONSTRAINT_SCHEMA, '') + '.' + rc.CONSTRAINT_TABLE_NAME ThisTableID,
COALESCE(rc.UNIQUE_CONSTRAINT_CATALOG, '') + '.' + COALESCE(rc.UNIQUE_CONSTRAINT_SCHEMA, '') + '.' + rc.UNIQUE_CONSTRAINT_TABLE_NAME OtherTableID,
rc.CONSTRAINT_NAME Name,
tc.COLUMN_NAME ThisColumn,
oc.COLUMN_NAME OtherColumn
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE tc ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE oc ON oc.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME
");

return data.ToList();
}

protected override string GetDatabaseName(DbConnection dbConnection)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Linq/SchemaProvider/SchemaProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Test(string context)
AssertType<Model.LinqDataTypes>(conn.MappingSchema, dbSchema);
AssertType<Model.Parent> (conn.MappingSchema, dbSchema);

// Assert.That(dbSchema.Tables.Single(t => t.TableName.ToLower() == "doctor").ForeignKeys.Count, Is.EqualTo(1));
Assert.That(dbSchema.Tables.Single(t => t.TableName.ToLower() == "doctor").ForeignKeys.Count, Is.EqualTo(1));

switch (context)
{
Expand Down

0 comments on commit 88c0e54

Please sign in to comment.