Skip to content

Commit 5b86ce2

Browse files
committed
Fix for Issue #43
1 parent 6cd9ff1 commit 5b86ce2

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

SQLite.CodeFirst.Console/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
55
</configSections>
66
<connectionStrings>
7-
<add name="footballDb" connectionString="data source=.\footballDb.sqlite" providerName="System.Data.SQLite" />
7+
<add name="footballDb" connectionString="data source=.\footballDb.sqlite;foreign keys=true" providerName="System.Data.SQLite" />
88
</connectionStrings>
99
<startup>
1010
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

SQLite.CodeFirst.Console/FootballDbContext.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public FootballDbContext()
1616

1717
protected override void OnModelCreating(DbModelBuilder modelBuilder)
1818
{
19+
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
20+
1921
ConfigureTeamEntity(modelBuilder);
2022
ConfigureStadionEntity(modelBuilder);
23+
ConfigureCoachEntity(modelBuilder);
2124
ConfigurePlayerEntity(modelBuilder);
2225

23-
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
24-
2526
var initializer = new FootballDbInitializer(modelBuilder);
2627
Database.SetInitializer(initializer);
2728
}
@@ -31,9 +32,14 @@ private static void ConfigureTeamEntity(DbModelBuilder modelBuilder)
3132
modelBuilder.Entity<Team>();
3233

3334
modelBuilder.Entity<Team>()
34-
.HasOptional(p => p.Coach)
35+
.HasRequired(t => t.Coach)
3536
.WithMany()
3637
.WillCascadeOnDelete(false);
38+
39+
modelBuilder.Entity<Team>()
40+
.HasRequired(t => t.Stadion)
41+
.WithRequiredPrincipal()
42+
.WillCascadeOnDelete(true);
3743
}
3844

3945
private static void ConfigureStadionEntity(DbModelBuilder modelBuilder)
@@ -45,8 +51,8 @@ private static void ConfigureCoachEntity(DbModelBuilder modelBuilder)
4551
{
4652
modelBuilder.Entity<Coach>()
4753
.HasRequired(p => p.Team)
48-
.WithMany()
49-
.WillCascadeOnDelete(true);
54+
.WithRequiredPrincipal(t => t.Coach)
55+
.WillCascadeOnDelete(false);
5056
}
5157

5258
private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)

SQLite.CodeFirst/Builder/CreateDatabaseStatementBuilder.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data.Entity.Core.Metadata.Edm;
33
using System.Linq;
44
using SQLite.CodeFirst.Statement;
5+
using SQLite.CodeFirst.Utility;
56

67
namespace SQLite.CodeFirst.Builder
78
{
@@ -30,7 +31,14 @@ private IEnumerable<CreateTableStatement> GetCreateTableStatements()
3031
ICollection<AssociationType> associationTypes =
3132
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entitySet.Name).ToList();
3233

33-
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypes);
34+
var b = associationTypes.Select(a => new AssociationTypeWrapper
35+
{
36+
AssociationType = a,
37+
FromTableName = edmModel.Container.GetEntitySetByName(a.Constraint.FromRole.Name, true).Table,
38+
ToTableName = edmModel.Container.GetEntitySetByName(a.Constraint.ToRole.Name, true).Table
39+
});
40+
41+
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, b);
3442
yield return tableStatementBuilder.BuildStatement();
3543
}
3644
}

SQLite.CodeFirst/Builder/CreateTableStatementBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System.Collections.Generic;
22
using System.Data.Entity.Core.Metadata.Edm;
3-
using SQLite.CodeFirst.Extensions;
43
using SQLite.CodeFirst.Statement;
4+
using SQLite.CodeFirst.Utility;
55

66
namespace SQLite.CodeFirst.Builder
77
{
88
internal class CreateTableStatementBuilder : IStatementBuilder<CreateTableStatement>
99
{
1010
private readonly EntitySet entitySet;
11-
private readonly IEnumerable<AssociationType> associationTypes;
11+
private readonly IEnumerable<AssociationTypeWrapper> associationTypes;
1212

13-
public CreateTableStatementBuilder(EntitySet entitySet, IEnumerable<AssociationType> associationTypes)
13+
public CreateTableStatementBuilder(EntitySet entitySet, IEnumerable<AssociationTypeWrapper> associationTypes)
1414
{
1515
this.entitySet = entitySet;
1616
this.associationTypes = associationTypes;

SQLite.CodeFirst/Builder/ForeignKeyStatementBuilder.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using System.Data.Entity.Core.Metadata.Edm;
33
using System.Linq;
44
using SQLite.CodeFirst.Statement;
5+
using SQLite.CodeFirst.Utility;
56

67
namespace SQLite.CodeFirst.Builder
78
{
89
internal class ForeignKeyStatementBuilder : IStatementBuilder<ColumnStatementCollection>
910
{
10-
private readonly IEnumerable<AssociationType> associationTypes;
11+
private readonly IEnumerable<AssociationTypeWrapper> associationTypes;
1112

12-
public ForeignKeyStatementBuilder(IEnumerable<AssociationType> associationTypes)
13+
public ForeignKeyStatementBuilder(IEnumerable<AssociationTypeWrapper> associationTypes)
1314
{
1415
this.associationTypes = associationTypes;
1516
}
@@ -26,10 +27,10 @@ private IEnumerable<ForeignKeyStatement> GetForeignKeyStatements()
2627
{
2728
yield return new ForeignKeyStatement
2829
{
29-
ForeignKey = associationType.Constraint.ToProperties.Select(x => x.Name),
30-
ForeignTable = associationType.Constraint.FromRole.Name,
31-
ForeignPrimaryKey = associationType.Constraint.FromProperties.Select(x => x.Name),
32-
CascadeDelete = associationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade
30+
ForeignKey = associationType.AssociationType.Constraint.ToProperties.Select(x => x.Name),
31+
ForeignTable = associationType.FromTableName,
32+
ForeignPrimaryKey = associationType.AssociationType.Constraint.FromProperties.Select(x => x.Name),
33+
CascadeDelete = associationType.AssociationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade
3334
};
3435
}
3536
}

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Reference Include="System.Data" />
7474
</ItemGroup>
7575
<ItemGroup>
76+
<Compile Include="Utility\AssociationTypeWrapper.cs" />
7677
<Compile Include="Builder\ColumnStatementCollectionBuilder.cs" />
7778
<Compile Include="Builder\CreateIndexStatementBuilder.cs" />
7879
<Compile Include="Builder\ForeignKeyStatementBuilder.cs" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Data.Entity.Core.Metadata.Edm;
2+
3+
namespace SQLite.CodeFirst.Utility
4+
{
5+
internal class AssociationTypeWrapper
6+
{
7+
public AssociationType AssociationType { get; set; }
8+
public string FromTableName { get; set; }
9+
public string ToTableName { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)