Skip to content

Commit 8125edc

Browse files
committed
Fix for #109.
1 parent 90a21c2 commit 8125edc

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace SQLite.CodeFirst.Console.Entity
6+
{
7+
/// <summary>
8+
/// See https://github.com/msallin/SQLiteCodeFirst/issues/109
9+
/// </summary>
10+
public class FooCompositeKey
11+
{
12+
[Key, Column(Order = 1)]
13+
public int Id { get; set; }
14+
15+
[Key, Column(Order = 2), StringLength(20)]
16+
public string Version { get; set; }
17+
18+
[StringLength(255)]
19+
public string Name { get; set; }
20+
21+
public virtual ICollection<FooRelationshipA> FooeyACollection { get; set; }
22+
23+
public virtual ICollection<FooRelationshipB> FooeyBCollection { get; set; }
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace SQLite.CodeFirst.Console.Entity
5+
{
6+
/// <summary>
7+
/// See https://github.com/msallin/SQLiteCodeFirst/issues/109
8+
/// </summary>
9+
public class FooRelationshipA
10+
{
11+
public int Id { get; set; }
12+
13+
[StringLength(255)]
14+
public string Name { get; set; }
15+
16+
public virtual ICollection<FooCompositeKey> Fooey { get; set; }
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace SQLite.CodeFirst.Console.Entity
5+
{
6+
/// <summary>
7+
/// See https://github.com/msallin/SQLiteCodeFirst/issues/109
8+
/// </summary>
9+
public class FooRelationshipB
10+
{
11+
public int Id { get; set; }
12+
13+
[StringLength(255)]
14+
public string Name { get; set; }
15+
16+
public virtual ICollection<FooCompositeKey> Fooey { get; set; }
17+
}
18+
}

SQLite.CodeFirst.Console/ModelConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void Configure(DbModelBuilder modelBuilder)
1212
ConfigureCoachEntity(modelBuilder);
1313
ConfigurePlayerEntity(modelBuilder);
1414
ConfigureSelfReferencingEntities(modelBuilder);
15+
ConfigureCompositeKeyEntities(modelBuilder);
1516
}
1617

1718
private static void ConfigureTeamEntity(DbModelBuilder modelBuilder)
@@ -54,5 +55,12 @@ private static void ConfigureSelfReferencingEntities(DbModelBuilder modelBuilder
5455
modelBuilder.Entity<FooSelf>();
5556
modelBuilder.Entity<FooStep>();
5657
}
58+
59+
private static void ConfigureCompositeKeyEntities(DbModelBuilder modelBuilder)
60+
{
61+
modelBuilder.Entity<FooCompositeKey>();
62+
modelBuilder.Entity<FooRelationshipA>();
63+
modelBuilder.Entity<FooRelationshipB>();
64+
}
5765
}
5866
}

SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
</Compile>
7575
<Compile Include="Entity\Coach.cs" />
7676
<Compile Include="Entity\CustomHistory.cs" />
77+
<Compile Include="Entity\FooCompositeKey.cs" />
78+
<Compile Include="Entity\FooRelationshipA.cs" />
79+
<Compile Include="Entity\FooRelationshipB.cs" />
7780
<Compile Include="Entity\IEntity.cs" />
7881
<Compile Include="Entity\Person.cs" />
7982
<Compile Include="Entity\Player.cs" />

SQLite.CodeFirst.Test/IntegrationTests/SqlGenerationTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class SqlGenerationTest
2121
CREATE TABLE ""Foos"" ([FooId] INTEGER PRIMARY KEY, [Name] nvarchar, [FooSelf1Id] int, [FooSelf2Id] int, [FooSelf3Id] int, FOREIGN KEY (FooSelf1Id) REFERENCES ""Foos""(FooId), FOREIGN KEY (FooSelf2Id) REFERENCES ""Foos""(FooId), FOREIGN KEY (FooSelf3Id) REFERENCES ""Foos""(FooId));
2222
CREATE TABLE ""FooSelves"" ([FooSelfId] INTEGER PRIMARY KEY, [FooId] int NOT NULL, [Number] int NOT NULL, FOREIGN KEY (FooId) REFERENCES ""Foos""(FooId) ON DELETE CASCADE);
2323
CREATE TABLE ""FooSteps"" ([FooStepId] INTEGER PRIMARY KEY, [FooId] int NOT NULL, [Number] int NOT NULL, FOREIGN KEY (FooId) REFERENCES ""Foos""(FooId) ON DELETE CASCADE);
24+
CREATE TABLE ""FooCompositeKeys"" ([Id] int NOT NULL, [Version] nvarchar (20) NOT NULL, [Name] nvarchar (255), PRIMARY KEY(Id, Version));
25+
CREATE TABLE ""FooRelationshipAs"" ([Id] INTEGER PRIMARY KEY, [Name] nvarchar (255));
26+
CREATE TABLE ""FooRelationshipBs"" ([Id] INTEGER PRIMARY KEY, [Name] nvarchar (255));
27+
CREATE TABLE ""FooRelationshipAFooCompositeKeys"" ([FooRelationshipA_Id] int NOT NULL, [FooCompositeKey_Id] int NOT NULL, [FooCompositeKey_Version] nvarchar (20) NOT NULL, PRIMARY KEY(FooRelationshipA_Id, FooCompositeKey_Id, FooCompositeKey_Version), FOREIGN KEY (FooRelationshipA_Id) REFERENCES ""FooRelationshipAs""(Id) ON DELETE CASCADE, FOREIGN KEY (FooCompositeKey_Id, FooCompositeKey_Version) REFERENCES ""FooCompositeKeys""(Id, Version) ON DELETE CASCADE);
28+
CREATE TABLE ""FooRelationshipBFooCompositeKeys"" ([FooRelationshipB_Id] int NOT NULL, [FooCompositeKey_Id] int NOT NULL, [FooCompositeKey_Version] nvarchar (20) NOT NULL, PRIMARY KEY(FooRelationshipB_Id, FooCompositeKey_Id, FooCompositeKey_Version), FOREIGN KEY (FooRelationshipB_Id) REFERENCES ""FooRelationshipBs""(Id) ON DELETE CASCADE, FOREIGN KEY (FooCompositeKey_Id, FooCompositeKey_Version) REFERENCES ""FooCompositeKeys""(Id, Version) ON DELETE CASCADE);
2429
CREATE INDEX ""IX_MyTable_Id"" ON ""MyTable"" (Id);
2530
CREATE INDEX ""IX_Team_TeamsName"" ON ""MyTable"" (Name);
2631
CREATE INDEX ""IX_TeamPlayer_Number"" ON ""TeamPlayer"" (Number);
@@ -32,7 +37,11 @@ public class SqlGenerationTest
3237
CREATE INDEX ""IX_Foo_FooSelf2Id"" ON ""Foos"" (FooSelf2Id);
3338
CREATE INDEX ""IX_Foo_FooSelf3Id"" ON ""Foos"" (FooSelf3Id);
3439
CREATE INDEX ""IX_FooSelf_FooId"" ON ""FooSelves"" (FooId);
35-
CREATE INDEX ""IX_FooStep_FooId"" ON ""FooSteps"" (FooId);";
40+
CREATE INDEX ""IX_FooStep_FooId"" ON ""FooSteps"" (FooId);
41+
CREATE INDEX ""IX_FooRelationshipAFooCompositeKey_FooRelationshipA_Id"" ON ""FooRelationshipAFooCompositeKeys"" (FooRelationshipA_Id);
42+
CREATE INDEX ""IX_FooRelationshipAFooCompositeKey_FooCompositeKey_Id_FooCompositeKey_Version"" ON ""FooRelationshipAFooCompositeKeys"" (FooCompositeKey_Id, FooCompositeKey_Version);
43+
CREATE INDEX ""IX_FooRelationshipBFooCompositeKey_FooRelationshipB_Id"" ON ""FooRelationshipBFooCompositeKeys"" (FooRelationshipB_Id);
44+
CREATE INDEX ""IX_FooRelationshipBFooCompositeKey_FooCompositeKey_Id_FooCompositeKey_Version"" ON ""FooRelationshipBFooCompositeKeys"" (FooCompositeKey_Id, FooCompositeKey_Version);";
3645

3746
private static string generatedSql;
3847

SQLite.CodeFirst/Internal/Convention/SqliteForeignKeyIndexConvention.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public virtual void Apply(AssociationType item, DbModel model)
4646
}
4747

4848
string tableName = item.Constraint.ToRole.GetEntityType().GetTableName();
49-
string propertyName = edmProperty.Name;
49+
50+
// Special treatment for composite primary keys
51+
string propertyName = item.Constraint.FromProperties.Count > 1
52+
? String.Join("_", item.Constraint.ToProperties)
53+
: edmProperty.Name;
5054

5155
// The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification
5256
// and the foreignKeyIncidies will be readded with the correct name.

0 commit comments

Comments
 (0)