-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
308 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using DataLayer.SchemaDb; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MyFirstEfCoreApp | ||
{ | ||
public class AppDbContext : DbContext | ||
{ | ||
private const string ConnectionString = //#A | ||
@"Server=(localdb)\mssqllocaldb; | ||
Database=MyFirstEfCoreDb; | ||
Trusted_Connection=True"; | ||
|
||
public DbSet<Book> Books { get; set; } | ||
|
||
protected override void OnConfiguring( | ||
DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlServer(ConnectionString); //#B | ||
} | ||
} | ||
|
||
/******************************************************** | ||
#A The connection string is used by the SQL Server database provider to find the database | ||
#B Using the SQL Server database provider’s UseSqlServer command sets up the options ready for creating the applications’s DBContext | ||
********************************************************/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
namespace DataLayer.SchemaDb | ||
{ | ||
public class Author | ||
{ | ||
public int AuthorId { get; set; } //#D | ||
public string Name { get; set; } | ||
public string WebUrl { get; set; } | ||
} | ||
|
||
/******************************************************* | ||
#D This holds the Primary Key of the Author row in the database | ||
* ************************************************/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace DataLayer.SchemaDb | ||
{ | ||
public class Book | ||
{ | ||
public int BookId { get; set; } | ||
|
||
public string Title { get; set; } | ||
public string Description { get; set; } | ||
public DateTime PublishedOn { get; set; } | ||
|
||
|
||
//----------------------------------------- | ||
//one-to-one relationships | ||
|
||
public int AuthorId { get; set; } | ||
public Author Author { get; set; } | ||
|
||
//many-to-many relationships | ||
public ICollection<Book> Books { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace DataLayer.SchemaDb | ||
{ | ||
public class Review | ||
{ | ||
public int ReviewId { get; set; } | ||
|
||
public int NumStars { get; set; } | ||
|
||
//----------------------------------------- | ||
//many-to-many relationships | ||
|
||
public ICollection<Book> Books { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DataLayer.SchemaDb; | ||
|
||
public class SchemaDbContext : DbContext | ||
{ | ||
public SchemaDbContext(DbContextOptions<SchemaDbContext> options) | ||
: base(options) { } | ||
|
||
public DbSet<Book> Books { get; set; } | ||
public DbSet<Author> Authors { get; set; } | ||
public DbSet<Review> Reviews { get; set; } | ||
|
||
protected override void | ||
OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Book>().ToTable("SchemaTest"); | ||
modelBuilder.Entity<Author>().ToTable("SchemaTest", "Schema1"); | ||
modelBuilder.Entity<Review>().ToTable("SchemaTest", "Schema2"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using DataLayer.MyEntityDb; | ||
using DataLayer.SchemaDb; | ||
using EfSchemaCompare; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore; | ||
using TestSupport.EfHelpers; | ||
using TestSupport.Helpers; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using Xunit.Extensions.AssertExtensions; | ||
|
||
namespace Test.UnitTests; | ||
|
||
public class TestSchemas | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public TestSchemas(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void CompareEfSqlServer() | ||
{ | ||
//SETUP | ||
var options = this.CreateUniqueClassOptions<SchemaDbContext>(); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var comparer = new CompareEfSql(); | ||
|
||
//ATTEMPT | ||
var hasErrors = comparer.CompareEfWithDb(context); | ||
|
||
//VERIFY | ||
hasErrors.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void CompareEfSqlServerExcludeTableWithDefaultSchema() | ||
{ | ||
//SETUP | ||
var options = this.CreateUniqueClassOptions<SchemaDbContext>(); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var config = new CompareEfSqlConfig | ||
{ | ||
TablesToIgnoreCommaDelimited = "SchemaTest" | ||
}; | ||
var comparer = new CompareEfSql(config); | ||
|
||
//ATTEMPT | ||
var hasErrors = comparer.CompareEfWithDb(context); | ||
|
||
//VERIFY | ||
_output.WriteLine(comparer.GetAllErrors); | ||
hasErrors.ShouldBeTrue(); | ||
comparer.GetAllErrors.ShouldEqual("NOT IN DATABASE: Entity 'Book', table name. Expected = SchemaTest"); | ||
} | ||
|
||
[Fact] | ||
public void CompareEfSqlServerExcludeTableWithSchema() | ||
{ | ||
//SETUP | ||
var options = this.CreateUniqueClassOptions<SchemaDbContext>(); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var config = new CompareEfSqlConfig | ||
{ | ||
TablesToIgnoreCommaDelimited = "Schema2.SchemaTest" | ||
}; | ||
var comparer = new CompareEfSql(config); | ||
|
||
//ATTEMPT | ||
var hasErrors = comparer.CompareEfWithDb(context); | ||
|
||
//VERIFY | ||
_output.WriteLine(comparer.GetAllErrors); | ||
hasErrors.ShouldBeTrue(); | ||
comparer.GetAllErrors.ShouldEqual("NOT IN DATABASE: Entity 'Review', table name. Expected = Schema2.SchemaTest"); | ||
} | ||
|
||
[Fact] | ||
public void CompareEfSqlServerExcludeMultipleTables() | ||
{ | ||
//SETUP | ||
var options = this.CreateUniqueClassOptions<SchemaDbContext>(); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var config = new CompareEfSqlConfig | ||
{ | ||
TablesToIgnoreCommaDelimited = "SchemaTest,Schema1.SchemaTest , Schema2.SchemaTest " | ||
}; | ||
var comparer = new CompareEfSql(config); | ||
|
||
//ATTEMPT | ||
var hasErrors = comparer.CompareEfWithDb(context); | ||
|
||
//VERIFY | ||
_output.WriteLine(comparer.GetAllErrors); | ||
hasErrors.ShouldBeTrue(); | ||
comparer.GetAllErrors.ShouldEqual(@"NOT IN DATABASE: Entity 'Author', table name. Expected = Schema1.SchemaTest | ||
NOT IN DATABASE: Entity 'Book', table name. Expected = SchemaTest | ||
NOT IN DATABASE: Entity 'Review', table name. Expected = Schema2.SchemaTest"); | ||
} | ||
|
||
[Fact] | ||
public void CompareEfSqlServerExcludeBadTable() | ||
{ | ||
//SETUP | ||
var options = this.CreateUniqueClassOptions<SchemaDbContext>(); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var config = new CompareEfSqlConfig | ||
{ | ||
TablesToIgnoreCommaDelimited = "BadTableName" | ||
}; | ||
var comparer = new CompareEfSql(config); | ||
|
||
//ATTEMPT | ||
try | ||
{ | ||
comparer.CompareEfWithDb(context); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.Message.ShouldEqual("The TablesToIgnoreCommaDelimited config property contains a table name of 'BadTableName', which was not found in the database"); | ||
return; | ||
} | ||
|
||
//VERIFY | ||
true.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void CompareEfPostgreSqlExcludeMultipleTables() | ||
{ | ||
//SETUP | ||
var options = this.CreatePostgreSqlUniqueClassOptions<SchemaDbContext>( | ||
builder => | ||
{ | ||
builder.UseNpgsql(this.GetUniquePostgreSqlConnectionString(), | ||
o => o.SetPostgresVersion(12, 0)); | ||
}); | ||
using var context = new SchemaDbContext(options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
var config = new CompareEfSqlConfig | ||
{ | ||
TablesToIgnoreCommaDelimited = "SchemaTest,Schema1.SchemaTest , Schema2.SchemaTest " | ||
}; | ||
var comparer = new CompareEfSql(config); | ||
|
||
//ATTEMPT | ||
var hasErrors = comparer.CompareEfWithDb(context); | ||
|
||
//VERIFY | ||
_output.WriteLine(comparer.GetAllErrors); | ||
hasErrors.ShouldBeTrue(); | ||
comparer.GetAllErrors.ShouldEqual(@"NOT IN DATABASE: Entity 'Author', table name. Expected = Schema1.SchemaTest | ||
NOT IN DATABASE: Entity 'Book', table name. Expected = SchemaTest | ||
NOT IN DATABASE: Entity 'Review', table name. Expected = Schema2.SchemaTest"); | ||
} | ||
} |