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

Handle primary keys not defined in SQL #8

Closed JeanCollas closed 5 years ago

JeanCollas commented 6 years ago

When primary key is not defined in SQL DB, many null exceptions are thrown instead of logging the error.

JonPSmith commented 6 years ago

Hi @JeanCollas,

Not defining a primary key in a class for EF Core would be caught when EF Core tries to build its model of the database. I don't think this a logging issue or an issue with the EfCore.TestSupport library.

JeanCollas commented 6 years ago

Actually I had Id keys missing on tables, the site runs well, but the script TestSupport crashes. If you set the field Id as key, EF Core considers that the Id field is a key, even if it is not the case in the DB and it doesn't check it (it still works, it just has performances issues).

JonPSmith commented 6 years ago

OK, now I understand a bit better. When you say that

the script TestSupport crashes.

Do you think there is a bug in EfCore.TestSupport? If so, in what area?

JeanCollas commented 6 years ago

Yes, you may try it by just temporary removing one primary key in the SQL. You will find that in Stage1Comparer, all statements like entityType.FindPrimaryKey().Relational().Name will break with a NullException (as the FindPrimaryKey() returns null), which is forwarded in CompareEfWithDb. And of course the method CompareForeignKeys never completes, but also CompareColumns which checks for the primary key.

JonPSmith commented 6 years ago

Thanks for the feedback - that make sense. I'm quite busy at the moment so I won't fix this at the moment. I will leave this issue open.

JonPSmith commented 5 years ago

hi @JeanCollas,

I have just released EfCore.TestSupport 1.5.2. In this I filter out DbQuery types, which was what caused the "no primary key" exception.