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
47 changes: 47 additions & 0 deletions EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using EntityFramework.Explained._Tools.Helpers;
using EntityFramework.Explained._Tools.TestContexts;
using Microsoft.EntityFrameworkCore;
using QuickPulse.Explains;
using QuickPulse.Explains.Text;

namespace EntityFramework.Explained.Schema.Conventions;

[DocFile]
public class UniqueConstraints
{
[Index(nameof(Name), IsUnique = true)]
public class Thing
{
public int Id { get; set; }
public string Name { get; set; } = default!;
}

[Index(nameof(Name), nameof(Id), IsUnique = true, IsDescending = new[] { false, true })]
public class CombinedThing
{
public int Id { get; set; }
public string Name { get; set; } = default!;
}

[Fact]
[DocHeader("Sql Server")]
[DocContent("Has unique index on Name.")]
public void SqlServer()
{
using var context = new TestSqlServerContext<Thing>();
var sql = context.Database.GenerateCreateScript();
var reader = LinesReader.FromText(sql);
Assert.Contains("CREATE UNIQUE INDEX [IX_Items_Name] ON [Items] ([Name]);", reader.SkipToLineContaining("INDEX"));
}

[Fact]
[DocHeader("Sqlite")]
[DocContent("Has combined unique index on Name and Id with a descending order on Id.")]
public void Sqlite()
{
using var context = new TestSqliteContext<CombinedThing>();
var sql = context.Database.GenerateCreateScript();
var reader = LinesReader.FromText(sql);
Assert.Contains("CREATE UNIQUE INDEX \"IX_Items_Name_Id\" ON \"Items\" (\"Name\", \"Id\" DESC);", reader.SkipToLineContaining("Index"));
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ has entity with a default index
#### Sql Server
has entity with combined sorted index
#### Sqlite
Generates `TEXT`.
### Unique Constraints
#### Sql Server
Has unique index.
#### Sqlite
Has combined unique index
has entity with a default index
#### Sqlite
has entity with combined sorted index
Expand Down
Loading