Skip to content

Commit 823c260

Browse files
committed
Support for the "MaxLength" annotation.
1 parent b9db2ac commit 823c260

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ This Project ships a `SqliteContextInitializer` which creates a new SQLite Datab
55
I started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub).
66

77
Currently the following is supported:
8-
- Tables from classes (with table annotation)
9-
- Columns from properties (with key annotation)
10-
- Auto increment (An int PrimaryKey will automatically be incremented)
8+
- Tables from classes (supported annotations: `Table`)
9+
- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`)
10+
- PrimaryKey constraint (`Key` annotation, key composites are supported)
1111
- ForeignKey constraint (1-n relationships, support for 'Cascade on delete')
1212
- Not Null constraint
13+
- Auto increment (An int PrimaryKey will automatically be incremented)
1314

1415
I tried to write the code in a extensible way.
1516
The logic is devided into two main parts. Builder and Statement.

SQLite.CodeFirst.Console/Entity/Player.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ namespace SQLite.CodeFirst.Console.Entity
66
public class Player
77
{
88
[Key]
9-
[Column(Order = 1)]
9+
[Column(Order = 1)]
1010
public string FirstName { get; set; }
1111
[Key]
12-
[Column(Order = 2)]
12+
[Column(Order = 2)]
1313
public string LastName { get; set; }
14+
[MaxLength(100)]
15+
public string Street { get; set; }
16+
[Required]
17+
public string City { get; set; }
1418
public Team Team { get; set; }
1519
}
1620
}

SQLite.CodeFirst.Console/TestDbContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
3030
{
3131
modelBuilder.RegisterEntityType(typeof(Player));
3232

33-
modelBuilder.Entity<Player>().Property(player => player.FirstName).HasMaxLength(10);
34-
3533
modelBuilder.Entity<Player>().ToTable("TeamPlayer");
3634

3735
modelBuilder.Entity<Player>()

SQLite.CodeFirst/Builder/ColumnStatementCollectionBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ private IEnumerable<ColumnStatement> CreateColumnStatements()
3232
ColumnConstraints = new ColumnConstraintCollection()
3333
};
3434

35+
AddMaxLengthConstraintIfNecessary(property, columnStatement);
3536
AdjustDatatypeForAutogenerationIfNecessary(property, columnStatement);
3637
AddNullConstraintIfNecessary(property, columnStatement);
3738

3839
yield return columnStatement;
3940
}
4041
}
4142

43+
private void AddMaxLengthConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
44+
{
45+
if (property.MaxLength.HasValue)
46+
{
47+
columnStatement.ColumnConstraints.Add(new MaxLengthConstraint(property.MaxLength.Value));
48+
}
49+
}
50+
4251
private static void AdjustDatatypeForAutogenerationIfNecessary(EdmProperty property, ColumnStatement columnStatement)
4352
{
4453
if (property.StoreGeneratedPattern == StoreGeneratedPattern.Identity)

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Builder\ForeignKeyStatementBuilder.cs" />
5454
<Compile Include="Builder\CreateDatabaseStatementBuilder.cs" />
5555
<Compile Include="Builder\PrimaryKeyStatementBuilder.cs" />
56+
<Compile Include="Statement\ColumnConstraint\MaxLengthConstraint.cs" />
5657
<Compile Include="Statement\PrimaryKeyStatement.cs" />
5758
<Compile Include="Properties\AssemblyInfo.cs" />
5859
<Compile Include="SqliteConnectionStringParser.cs" />
@@ -69,7 +70,6 @@
6970
<Compile Include="Statement\CreateTableStatement.cs" />
7071
<Compile Include="Statement\IStatement.cs" />
7172
<Compile Include="Builder\CreateTableStatementBuilder.cs" />
72-
<Compile Include="Statement\ColumnConstraint\PrimaryKeyConstraint.cs" />
7373
</ItemGroup>
7474
<ItemGroup>
7575
<None Include="packages.config" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace SQLite.CodeFirst.Statement.ColumnConstraint
4+
{
5+
internal class MaxLengthConstraint : IColumnConstraint
6+
{
7+
private const string Template = "({max-length})";
8+
9+
public int? MaxLength { get; set; }
10+
11+
public MaxLengthConstraint() { }
12+
13+
public MaxLengthConstraint(int maxLength)
14+
{
15+
MaxLength = maxLength;
16+
}
17+
18+
public string CreateStatement()
19+
{
20+
if (MaxLength == null)
21+
{
22+
throw new InvalidOperationException("MaxLength must not be null!");
23+
}
24+
25+
return Template.Replace("{max-length}", MaxLength.ToString());
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)