Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SQLite.CodeFirst.Console/Entity/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class Player : Person
public int Number { get; set; }

public virtual Team Team { get; set; }

public virtual Player Mentor { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using SQLite.CodeFirst.Builder.NameCreators;
using SQLite.CodeFirst.Statement;
using SQLite.CodeFirst.Utility;

Expand All @@ -27,29 +26,11 @@ public CreateDatabaseStatement BuildStatement()

private IEnumerable<CreateTableStatement> GetCreateTableStatements()
{
var associationTypeContainer = new AssociationTypeContainer(edmModel.AssociationTypes, edmModel.Container);

foreach (var entitySet in edmModel.Container.EntitySets)
{
ICollection<AssociationType> associationTypes =
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entitySet.Name).ToList();

IList<AssociationTypeWrapper> associationTypeWrappers = new List<AssociationTypeWrapper>();
foreach (var associationType in associationTypes)
{
string fromTable = edmModel.Container.GetEntitySetByName(associationType.Constraint.FromRole.Name, true).Table;
string toTable = edmModel.Container.GetEntitySetByName(associationType.Constraint.ToRole.Name, true).Table;

string fromTableName = TableNameCreator.CreateTableName(fromTable);
string toTableName = TableNameCreator.CreateTableName(toTable);

associationTypeWrappers.Add(new AssociationTypeWrapper
{
AssociationType = associationType,
FromTableName = fromTableName,
ToTableName = toTableName
});
}

var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypeWrappers);
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypeContainer);
yield return tableStatementBuilder.BuildStatement();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure.Annotations;
using System.Globalization;
using System.Linq;
using SQLite.CodeFirst.Builder.NameCreators;
using SQLite.CodeFirst.Extensions;
Expand Down
11 changes: 5 additions & 6 deletions SQLite.CodeFirst/Internal/Builder/CreateTableStatementBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using SQLite.CodeFirst.Builder.NameCreators;
using SQLite.CodeFirst.Statement;
Expand All @@ -10,19 +9,19 @@ namespace SQLite.CodeFirst.Builder
internal class CreateTableStatementBuilder : IStatementBuilder<CreateTableStatement>
{
private readonly EntitySet entitySet;
private readonly IEnumerable<AssociationTypeWrapper> associationTypes;
private readonly AssociationTypeContainer associationTypeContainer;

public CreateTableStatementBuilder(EntitySet entitySet, IEnumerable<AssociationTypeWrapper> associationTypes)
public CreateTableStatementBuilder(EntitySet entitySet, AssociationTypeContainer associationTypeContainer)
{
this.entitySet = entitySet;
this.associationTypes = associationTypes;
this.associationTypeContainer = associationTypeContainer;
}

public CreateTableStatement BuildStatement()
{
var simpleColumnCollection = new ColumnStatementCollectionBuilder(entitySet.ElementType.Properties).BuildStatement();
var primaryKeyStatement = new PrimaryKeyStatementBuilder(entitySet.ElementType.KeyMembers).BuildStatement();
var foreignKeyCollection = new ForeignKeyStatementBuilder(associationTypes).BuildStatement();
var foreignKeyCollection = new ForeignKeyStatementBuilder(associationTypeContainer.GetAssociationTypes(entitySet.Name)).BuildStatement();

var columnStatements = new List<IStatement>();
columnStatements.AddRange(simpleColumnCollection);
Expand Down
11 changes: 5 additions & 6 deletions SQLite.CodeFirst/Internal/Builder/ForeignKeyStatementBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using SQLite.CodeFirst.Statement;
using SQLite.CodeFirst.Utility;
Expand All @@ -8,9 +7,9 @@ namespace SQLite.CodeFirst.Builder
{
internal class ForeignKeyStatementBuilder : IStatementBuilder<ColumnStatementCollection>
{
private readonly IEnumerable<AssociationTypeWrapper> associationTypes;
private readonly IEnumerable<SqliteAssociationType> associationTypes;

public ForeignKeyStatementBuilder(IEnumerable<AssociationTypeWrapper> associationTypes)
public ForeignKeyStatementBuilder(IEnumerable<SqliteAssociationType> associationTypes)
{
this.associationTypes = associationTypes;
}
Expand All @@ -27,10 +26,10 @@ private IEnumerable<ForeignKeyStatement> GetForeignKeyStatements()
{
yield return new ForeignKeyStatement
{
ForeignKey = associationType.AssociationType.Constraint.ToProperties.Select(x => x.Name),
ForeignKey = associationType.ForeignKey,
ForeignTable = associationType.FromTableName,
ForeignPrimaryKey = associationType.AssociationType.Constraint.FromProperties.Select(x => x.Name),
CascadeDelete = associationType.AssociationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade
ForeignPrimaryKey = associationType.ForeignPrimaryKey,
CascadeDelete = associationType.CascadeDelete
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class TableNameCreator
{
public static string CreateTableName(string tableFromEntitySet)
{
tableFromEntitySet = tableFromEntitySet.Trim(SpecialChars.EscapeCharOpen, SpecialChars.EscapeCharClose);
return String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", SpecialChars.EscapeCharOpen, tableFromEntitySet, SpecialChars.EscapeCharClose);
}
}
Expand Down
21 changes: 21 additions & 0 deletions SQLite.CodeFirst/Internal/Utility/AssociationTypeContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;

namespace SQLite.CodeFirst.Utility
{
internal class AssociationTypeContainer
{
private readonly IEnumerable<SqliteAssociationType> sqliteAssociationTypes;

public AssociationTypeContainer(IEnumerable<AssociationType> associationTypes, EntityContainer container)
{
sqliteAssociationTypes = associationTypes.Select(associationType => new SqliteAssociationType(associationType, container));
}

public IEnumerable<SqliteAssociationType> GetAssociationTypes(string entitySetName)
{
return sqliteAssociationTypes.Where(associationType => associationType.ToRoleEntitySetName == entitySetName);
}
}
}
11 changes: 0 additions & 11 deletions SQLite.CodeFirst/Internal/Utility/AssociationTypeWrapper.cs

This file was deleted.

50 changes: 50 additions & 0 deletions SQLite.CodeFirst/Internal/Utility/SqliteAssociationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using SQLite.CodeFirst.Builder.NameCreators;

namespace SQLite.CodeFirst.Utility
{
internal class SqliteAssociationType
{
private const string SelfReferencingPostfix = "Self";

public SqliteAssociationType(AssociationType associationType, EntityContainer container)
{
FromRoleEntitySetName = associationType.Constraint.FromRole.Name;
ToRoleEntitySetName = associationType.Constraint.ToRole.Name;

string fromTable = container.GetEntitySetByName(FromRoleEntitySetName, true).Table;
string toTable;

if (IsSelfReferencing(associationType))
{
toTable = fromTable;
ToRoleEntitySetName = FromRoleEntitySetName;
}
else
{
toTable = container.GetEntitySetByName(ToRoleEntitySetName, true).Table;
}

FromTableName = TableNameCreator.CreateTableName(fromTable);
ToTableName = TableNameCreator.CreateTableName(toTable);
ForeignKey = associationType.Constraint.ToProperties.Select(x => x.Name);
ForeignPrimaryKey = associationType.Constraint.FromProperties.Select(x => x.Name);
CascadeDelete = associationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade;
}

private static bool IsSelfReferencing(AssociationType associationType)
{
return associationType.Constraint.ToRole.Name.Remove(associationType.Constraint.ToRole.Name.Length - SelfReferencingPostfix.Length, SelfReferencingPostfix.Length) == associationType.Constraint.FromRole.Name;
}

public string ToRoleEntitySetName { get; set; }
public string FromRoleEntitySetName { get; set; }
public IEnumerable<string> ForeignKey { get; }
public string FromTableName { get; }
public string ToTableName { get; }
public IEnumerable<string> ForeignPrimaryKey { get; }
public bool CascadeDelete { get; }
}
}
3 changes: 2 additions & 1 deletion SQLite.CodeFirst/SQLite.CodeFirst.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@
<Compile Include="IDatabaseCreator.cs" />
<Compile Include="Internal\Utility\HashCreator.cs" />
<Compile Include="Internal\Utility\HistoryEntityTypeValidator.cs" />
<Compile Include="Internal\Utility\SqliteAssociationType.cs" />
<Compile Include="ISqliteSqlGenerator.cs" />
<Compile Include="DbInitializers\SqliteDropCreateDatabaseWhenModelChanges.cs" />
<Compile Include="Entities\History.cs" />
<Compile Include="SqliteSqlGenerator.cs" />
<Compile Include="Internal\Statement\ColumnConstraint\IColumnConstraintCollection.cs" />
<Compile Include="Internal\Statement\IStatementCollection.cs" />
<Compile Include="Internal\Utility\AssociationTypeWrapper.cs" />
<Compile Include="Internal\Utility\AssociationTypeContainer.cs" />
<Compile Include="Internal\Builder\ColumnStatementCollectionBuilder.cs" />
<Compile Include="Internal\Builder\CreateIndexStatementBuilder.cs" />
<Compile Include="Internal\Builder\ForeignKeyStatementBuilder.cs" />
Expand Down