Hi guys! I'm unable to make Identity Server 4 works with MySQL, can you help me?
Hi guys!
I've been trying to learn how to use Identity Server 4 with EF, ASP.NET Core Identity and MySQL but have some issues I haven't been able to fix for a couple of hours and I'm honestly lost.
I'm getting an InvalidOperationException when calling context.Clients.Any() in my database Initializer
The Code:
private static void EnsureSeedData(ConfigurationDbContext context) { if (!context.Clients.Any()) //<--- Exception in this line { Console.WriteLine("Clients being populated"); foreach (var client in Config.GetClients().ToList()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } else { Console.WriteLine("Clients already populated"); } //...... }
The Exception
System.InvalidOperationException: No coercion operator is defined between types 'System.Int16' and 'System.Boolean
The Log:
info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT CASE WHEN EXISTS ( SELECT 1 FROM `Clients` AS `c`) THEN ('1') ELSE ('0') END fail: Microsoft.EntityFrameworkCore.Query[10100] An exception occurred in the database while iterating the results of a query for context type 'IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext'. System.InvalidOperationException: No coercion operator is defined between types 'System.Int16' and 'System.Boolean'. at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
For what I can understand, EF expected a value assignable to System.Boolean as result to the query that LINQ produce to evaluate Any() but is getting an System.Int16 and doesn't know how to cast it to Boolean. Is there any way I can't define the coercion operator so that LINQ does not fail?
Thanks in advance to everyone for taking the time to read this.
Full log here
All the code in this github repo
Let me tell you a bit about how do I get here, in case I missed something while configuring the application.
I followed the Identity Server 4 documentation and was able to make it work when using SQL Server as at the end of this page.
After this, following the instructions by Panagiotis Kanavos in this post, I installed MySQL.Data and MySql.Data.EntityFrameworkCore (both version 8.0.13), changed configuration in Startup.cs for ApplicationDbContext, PersistedGrantDbContext and ConfigurationDbContext to use MySQL instead of SQL Server; I also generate again the initial migration for all three context and ran them successfully.
At this point I test the Register form worked fine,and it did.
Finally I added the InitializeDatabase method in order to insert some clients and api resources on the database if they did not exists. It in this step that I detect the problem.When the Db Initializer executes, there is call to the LINQ Any() function that fails.
0 comments:
Post a Comment