diff --git a/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs new file mode 100644 index 0000000..46d8c10 --- /dev/null +++ b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs @@ -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(); + 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(); + 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")); + } +} \ No newline at end of file diff --git a/README.md b/README.md index f6a0353..065cd80 100644 --- a/README.md +++ b/README.md @@ -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