Skip to content

Commit 4f6c7f3

Browse files
committed
Implementation for schema.
1 parent 83167df commit 4f6c7f3

File tree

10 files changed

+113
-18
lines changed

10 files changed

+113
-18
lines changed

SQLite.CodeFirst.Console/FootballDbContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
2929

3030
private static void ConfigureTeamEntity(DbModelBuilder modelBuilder)
3131
{
32-
modelBuilder.Entity<Team>();
33-
34-
modelBuilder.Entity<Team>()
32+
modelBuilder.Entity<Team>().ToTable("MyTable", "mySchema")
3533
.HasRequired(t => t.Coach)
3634
.WithMany()
3735
.WillCascadeOnDelete(false);

SQLite.CodeFirst/Builder/CreateDatabaseStatementBuilder.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Data.Entity.Core.Metadata.Edm;
33
using System.Linq;
4+
using SQLite.CodeFirst.Builder.NameCreators;
45
using SQLite.CodeFirst.Statement;
56
using SQLite.CodeFirst.Utility;
67

@@ -31,14 +32,27 @@ private IEnumerable<CreateTableStatement> GetCreateTableStatements()
3132
ICollection<AssociationType> associationTypes =
3233
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entitySet.Name).ToList();
3334

34-
var b = associationTypes.Select(a => new AssociationTypeWrapper
35+
IList<AssociationTypeWrapper> associationTypeWrappers = new List<AssociationTypeWrapper>();
36+
foreach (var associationType in associationTypes)
3537
{
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-
});
38+
string fromSchema = edmModel.Container.GetEntitySetByName(associationType.Constraint.FromRole.Name, true).Schema;
39+
string fromName = edmModel.Container.GetEntitySetByName(associationType.Constraint.FromRole.Name, true).Table;
4040

41-
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, b);
41+
string toSchema = edmModel.Container.GetEntitySetByName(associationType.Constraint.ToRole.Name, true).Schema;
42+
string toName = edmModel.Container.GetEntitySetByName(associationType.Constraint.ToRole.Name, true).Table;
43+
44+
string fromTableName = TableNameCreator.CreateTableName(fromSchema, fromName);
45+
string toTableName = TableNameCreator.CreateTableName(toSchema, toName);
46+
47+
associationTypeWrappers.Add(new AssociationTypeWrapper
48+
{
49+
AssociationType = associationType,
50+
FromTableName = fromTableName,
51+
ToTableName = toTableName
52+
});
53+
}
54+
55+
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypeWrappers);
4256
yield return tableStatementBuilder.BuildStatement();
4357
}
4458
}

SQLite.CodeFirst/Builder/CreateIndexStatementBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Data.Entity.Infrastructure.Annotations;
77
using System.Globalization;
88
using System.Linq;
9+
using SQLite.CodeFirst.Builder.NameCreators;
910
using SQLite.CodeFirst.Extensions;
1011
using SQLite.CodeFirst.Statement;
1112

@@ -40,7 +41,7 @@ public CreateIndexStatementCollection BuildStatement()
4041
{
4142
IsUnique = index.IsUnique,
4243
Name = indexName,
43-
Table = entitySet.Table,
44+
Table = TableNameCreator.CreateTableName(entitySet.Schema, entitySet.Table),
4445
Columns = new Collection<CreateIndexStatement.IndexColumn>()
4546
};
4647
createIndexStatments.Add(indexName, createIndexStatement);
@@ -59,7 +60,7 @@ public CreateIndexStatementCollection BuildStatement()
5960

6061
private string GetIndexName(IndexAttribute index, EdmProperty property)
6162
{
62-
return index.Name ?? String.Format(CultureInfo.InvariantCulture, "IX_{0}_{1}", entitySet.ElementType.GetTableName(), property.Name);
63+
return index.Name ?? IndexNameCreator.CreateIndexName(entitySet.ElementType.GetTableName(), property.Name);
6364
}
6465
}
6566
}

SQLite.CodeFirst/Builder/CreateTableStatementBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Data.Entity.Core.Metadata.Edm;
4+
using SQLite.CodeFirst.Builder.NameCreators;
35
using SQLite.CodeFirst.Statement;
46
using SQLite.CodeFirst.Utility;
57

@@ -29,7 +31,7 @@ public CreateTableStatement BuildStatement()
2931

3032
return new CreateTableStatement
3133
{
32-
TableName = entitySet.Table,
34+
TableName = TableNameCreator.CreateTableName(entitySet.Schema, entitySet.Table),
3335
ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
3436
};
3537
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace SQLite.CodeFirst.Builder.NameCreators
5+
{
6+
internal static class IndexNameCreator
7+
{
8+
public const string IndexNamePrefix = "IX_";
9+
10+
public static string CreateIndexName(string tableName, string propertyName)
11+
{
12+
// If the tableName is escaped it means that this name contains special chars e.g. a dot (base.myTable)
13+
// Because the tablename is used to build the index name the index name must also be escaped.
14+
bool escaped = tableName.StartsWith(SpecialChars.EscapeCharOpen.ToString()) &&
15+
tableName.EndsWith(SpecialChars.EscapeCharClose.ToString());
16+
17+
tableName = tableName.Trim(SpecialChars.EscapeCharOpen, SpecialChars.EscapeCharClose);
18+
19+
string prefixChar = String.Empty;
20+
string postfixChar = String.Empty;
21+
if (escaped)
22+
{
23+
prefixChar = SpecialChars.EscapeCharOpen.ToString();
24+
postfixChar = SpecialChars.EscapeCharClose.ToString();
25+
}
26+
27+
return String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}_{3}{4}", prefixChar, IndexNamePrefix, tableName, propertyName, postfixChar);
28+
}
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SQLite.CodeFirst.Builder.NameCreators
2+
{
3+
internal class SpecialChars
4+
{
5+
public const char EscapeCharOpen = '\"';
6+
public const char EscapeCharClose = '\"';
7+
}
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace SQLite.CodeFirst.Builder.NameCreators
5+
{
6+
internal static class TableNameCreator
7+
{
8+
public static string CreateTableName(string entityName)
9+
{
10+
return CreateTableName(null, entityName);
11+
}
12+
13+
public static string CreateTableName(string schema, string entityName)
14+
{
15+
if (!String.IsNullOrWhiteSpace(schema) && schema != "dbo")
16+
{
17+
return String.Format(CultureInfo.InvariantCulture, "{0}{1}.{2}{3}", SpecialChars.EscapeCharOpen, schema, entityName, SpecialChars.EscapeCharClose);
18+
}
19+
20+
return entityName;
21+
}
22+
}
23+
}

SQLite.CodeFirst/Convention/SqliteForeignKeyIndexConvention.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Data.Entity.ModelConfiguration.Conventions;
99
using System.Globalization;
1010
using System.Linq;
11+
using SQLite.CodeFirst.Builder.NameCreators;
1112

1213
namespace SQLite.CodeFirst.Convention
1314
{
@@ -69,7 +70,7 @@ public virtual void Apply(AssociationType item, DbModel model)
6970

7071
private static IndexAnnotation CreateIndexAnnotation(string tableName, string propertyName, int count)
7172
{
72-
var indexName = String.Format(CultureInfo.InvariantCulture, "IX_{0}_{1}", tableName, propertyName);
73+
var indexName = IndexNameCreator.CreateIndexName(tableName, propertyName);
7374

7475
// If there are two Indicies on the same property, the count is added.
7576
// In SQLite an Index name must be global unique.
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Data.Entity.Core.Metadata.Edm;
1+
using System;
2+
using System.Data.Entity.Core.Metadata.Edm;
3+
using SQLite.CodeFirst.Builder.NameCreators;
4+
using SQLite.CodeFirst.Utility;
25

36
namespace SQLite.CodeFirst.Extensions
47
{
@@ -7,12 +10,24 @@ internal static class EntityTypeExtension
710
public static string GetTableName(this EntityType entityType)
811
{
912
MetadataProperty metadataProperty;
10-
if (entityType.MetadataProperties.TryGetValue("TableName", false, out metadataProperty))
13+
if (!entityType.MetadataProperties.TryGetValue("TableName", false, out metadataProperty))
1114
{
12-
return metadataProperty.Value.ToString();
15+
return entityType.Name;
1316
}
1417

15-
return entityType.Name;
18+
if (metadataProperty.Value.GetType().Name != "DatabaseName")
19+
{
20+
return entityType.Name;
21+
}
22+
23+
object metadataPropertyValue = metadataProperty.Value;
24+
Type metadataPropertyValueType = metadataProperty.Value.GetType();
25+
26+
// The type DatabaseName is internal. So we need reflection...
27+
var schema = (string)metadataPropertyValueType.GetProperty("Schema").GetValue(metadataPropertyValue);
28+
var name = (string)metadataPropertyValueType.GetProperty("Name").GetValue(metadataPropertyValue);
29+
30+
return TableNameCreator.CreateTableName(schema, name);
1631
}
1732
}
1833
}

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
<Reference Include="System.Data" />
7474
</ItemGroup>
7575
<ItemGroup>
76+
<Compile Include="Builder\NameCreators\IndexNameCreator.cs" />
77+
<Compile Include="Builder\NameCreators\SpecialChars.cs" />
7678
<Compile Include="Statement\ColumnConstraint\IColumnConstraintCollection.cs" />
7779
<Compile Include="Statement\IStatementCollection.cs" />
7880
<Compile Include="Utility\AssociationTypeWrapper.cs" />
@@ -104,6 +106,7 @@
104106
<Compile Include="Statement\CreateTableStatement.cs" />
105107
<Compile Include="Statement\IStatement.cs" />
106108
<Compile Include="Builder\CreateTableStatementBuilder.cs" />
109+
<Compile Include="Builder\NameCreators\TableNameCreator.cs" />
107110
</ItemGroup>
108111
<ItemGroup>
109112
<None Include="packages.config" />

0 commit comments

Comments
 (0)