From a1ab659d4a784e05d0c12c4a74bd341e9bc8ec1f Mon Sep 17 00:00:00 2001 From: abigailvp Date: Tue, 12 Aug 2025 09:49:03 +0200 Subject: [PATCH 1/2] feat(Schema): Indexes/UniqueConstraints: adding 2 tests #15 Indexes/DefaultIndexNames: removing unnecessary usings #11 --- .../Schema/Indexes/UniqueConstraints.cs | 49 +++++++++++++++++++ README.md | 10 ++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs diff --git a/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs new file mode 100644 index 0000000..26041e7 --- /dev/null +++ b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs @@ -0,0 +1,49 @@ +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 ef63dd5..724070b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,11 @@ entityTypeBuilder.Property(c => c.StringListProperty) Generates `nvarchar(max)`. #### Sqlite Generates `TEXT`. +### Unique Constraints +#### Sql Server +Has unique index. +#### Sqlite +Has combined unique index ### Class Nullability #### Sql Server `NullThing` Generates `NULL`. @@ -44,8 +49,8 @@ Generates `TEXT`. #### Sq Lite `NullThing` Generates `NULL`. `SomeThing` Generates `NOT NULL`. -`SomeThingId` have parameter in the foreign key definition -`NullThing` don't have parameter in the foreign key definition +`SomeThingId` adds `ON DELETE CASCADE` to the foreign key definition +`NullThing` does not add `ON DELETE CASCADE` to the foreign key definition ### Int Nullability #### Sql Server `int` Generates `int NOT NULL`. @@ -53,7 +58,6 @@ Generates `TEXT`. #### Sqlite `int` Generates `INTEGER NOT NULL`. `int?` Generates `INTEGER NULL` ->>>>>>> e63269a (updated readme.md) ### String Nullability #### Sql Server `string` Generates `NOT NULL`. From cba639204ac06b37fd217dd33bf3b2430b312da2 Mon Sep 17 00:00:00 2001 From: abigailvp Date: Tue, 12 Aug 2025 16:32:23 +0200 Subject: [PATCH 2/2] feat(schema): removing whitespace #15 --- EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs index 26041e7..46d8c10 100644 --- a/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs +++ b/EntityFramework.Explained/Schema/Indexes/UniqueConstraints.cs @@ -12,7 +12,6 @@ public class UniqueConstraints [Index(nameof(Name), IsUnique = true)] public class Thing { - public int Id { get; set; } public string Name { get; set; } = default!; } @@ -20,7 +19,6 @@ public class Thing [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!; }