Skip to content

Commit b0d5d62

Browse files
author
Marc Sallin
committed
42: Use CollateConstraint and refactoring to make it easier to use attributes as custom annotations.
1 parent 3689a3a commit b0d5d62

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

SQLite.CodeFirst/DbInitializers/SqliteInitializerBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SQLite.CodeFirst.Convention;
55
using System.IO;
66
using System.Linq;
7+
using SQLite.CodeFirst.Extensions;
78
using SQLite.CodeFirst.Utility;
89

910
namespace SQLite.CodeFirst
@@ -37,9 +38,11 @@ protected SqliteInitializerBase(DbModelBuilder modelBuilder)
3738
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
3839
modelBuilder.Conventions.Remove<TimestampAttributeConvention>();
3940

40-
modelBuilder.Properties()
41-
.Having(x => x.GetCustomAttributes(false).OfType<UniqueAttribute>().FirstOrDefault())
42-
.Configure((config, attribute) => config.HasColumnAnnotation("IsUnique", attribute));
41+
// There is some functionality which is supported by SQLite which can not be covered
42+
// by using the data annotation attributes from the .net framework.
43+
// So there are some custom attributes.
44+
modelBuilder.RegisterAttributeAsColumnAnnotation<UniqueAttribute>();
45+
modelBuilder.RegisterAttributeAsColumnAnnotation<CollateAttribute>();
4346

4447
// By default there is a 'ForeignKeyIndexConvention' but it can be removed.
4548
// And there is no "Contains" and no way to enumerate the ConventionsCollection.

SQLite.CodeFirst/Internal/Builder/ColumnStatementCollectionBuilder.cs

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

@@ -37,6 +38,7 @@ private IEnumerable<ColumnStatement> CreateColumnStatements()
3738
AdjustDatatypeForAutogenerationIfNecessary(property, columnStatement);
3839
AddNullConstraintIfNecessary(property, columnStatement);
3940
AddUniqueConstraintIfNecessary(property, columnStatement);
41+
AddCollationConstraintIfNecessary(property, columnStatement);
4042

4143
yield return columnStatement;
4244
}
@@ -68,17 +70,21 @@ private static void AddNullConstraintIfNecessary(EdmProperty property, ColumnSta
6870
}
6971
}
7072

73+
private static void AddCollationConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
74+
{
75+
var value = property.GetCustomAnnotation<CollateAttribute>();
76+
if (value != null)
77+
{
78+
columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.Collation });
79+
}
80+
}
81+
7182
private static void AddUniqueConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
7283
{
73-
MetadataProperty item;
74-
bool found = property.MetadataProperties.TryGetValue("http://schemas.microsoft.com/ado/2013/11/edm/customannotation:IsUnique", true, out item);
75-
if (found)
84+
var value = property.GetCustomAnnotation<UniqueAttribute>();
85+
if (value != null)
7686
{
77-
var value = (UniqueAttribute)item.Value;
78-
columnStatement.ColumnConstraints.Add(new UniqueConstraint
79-
{
80-
OnConflict = value.OnConflict
81-
});
87+
columnStatement.ColumnConstraints.Add(new UniqueConstraint { OnConflict = value.OnConflict });
8288
}
8389
}
8490
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Data.Entity;
2+
using System.Linq;
3+
4+
namespace SQLite.CodeFirst.Extensions
5+
{
6+
internal static class DbModelBuilderExtensions
7+
{
8+
public static void RegisterAttributeAsColumnAnnotation<TAttribute>(this DbModelBuilder modelBuilder)
9+
where TAttribute : class
10+
{
11+
modelBuilder.Properties()
12+
.Having(x => x.GetCustomAttributes(false).OfType<TAttribute>().FirstOrDefault())
13+
.Configure((config, attribute) => config.HasColumnAnnotation(typeof(TAttribute).Name, attribute));
14+
}
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Data.Entity.Core.Metadata.Edm;
3+
4+
namespace SQLite.CodeFirst.Extensions
5+
{
6+
internal static class EdmPropertyExtensions
7+
{
8+
public static TAttribute GetCustomAnnotation<TAttribute>(this EdmProperty property)
9+
where TAttribute : Attribute
10+
{
11+
MetadataProperty item;
12+
bool found = property.MetadataProperties.TryGetValue("http://schemas.microsoft.com/ado/2013/11/edm/customannotation:" + typeof(TAttribute).Name, true, out item);
13+
if (found)
14+
{
15+
return (TAttribute) item.Value;
16+
}
17+
18+
return null;
19+
}
20+
}
21+
}

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
<Compile Include="Internal\Builder\NameCreators\IndexNameCreator.cs" />
9292
<Compile Include="Internal\Builder\NameCreators\SpecialChars.cs" />
9393
<Compile Include="IDatabaseCreator.cs" />
94+
<Compile Include="Internal\Extensions\DbModelBuilderExtensions.cs" />
95+
<Compile Include="Internal\Extensions\EdmPropertyExtensions.cs" />
9496
<Compile Include="Internal\Statement\ColumnConstraint\CollateConstraint.cs" />
9597
<Compile Include="Internal\Statement\ColumnConstraint\UniqueConstraint.cs" />
9698
<Compile Include="Internal\Utility\HashCreator.cs" />

0 commit comments

Comments
 (0)