JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
352 stars 53 forks source link

Missing primary key in SQL table throws NullReferenceException #32

Closed adamjez closed 4 years ago

adamjez commented 4 years ago

Hi,

I ran into minor bug with nuget EfCore.TestSupport 3.1.1 version (latest).

I get unhandled NullReferenceException thrown in Stage1Comparer.CompareModelToDatabase(DatabaseModel databaseModel) line 297.

The problem occurs when a primary key is missing in a SQL table but is present in EF Core model.

repro:

CREATE TABLE [MyEntities] (
    [MyEntityId] int NOT NULL
);
public class MyEntity
{
    public int MyEntityId { get; set; }
}
...
public DbSet<MyEntity> MyEntities { get; set; }

I would expect a warning message explaining missing primary key in a SQL table


I found out that there is similar issue #8 but is closed. The issue was fixed with check for primary key in EF Core model. IMO similar check should be for SQL model.

JonPSmith commented 4 years ago

Hi @adamjez,

Yep, I see that. I won't fix it now but I've added it to the ToDo list. I have some other things to look at first.

Thanks for letting me know.

JonPSmith commented 4 years ago

I have put a fix in for missing primary keys in the database, but can you check this as my unit test didn't fail in the same way your code its.

I will close for now. Please confirm that its working for you.

LouisMilesRH commented 4 years ago

Working perfectly for me now 👍

adamjez commented 4 years ago

I tested it and it resolved the issue. Now I'm getting detailed assert message :-) Thank you!

JonPSmith commented 4 years ago

Thanks @LouisMilesRH and @adamjez for your feedback. Glad it solved your problem @adamjez and partly helped you @LouisMilesRH