Click here to Skip to main content
14,218,216 members
Rate this:
Please Sign up or sign in to vote.
Following on from last weeks tangle on key lengths, I need some advice on coercion operators between types using MySql.

Part of last week's trouble was that I failed to adequately explain what I needed so hopefully I will not fall into that trap this time.

To recap, I am attempting to use the combined example from the Identity Server site but targetting MySql rather than SqlServer (Where I note a fine new article from HHerzl).

Due to how MySql holds Booleans in Integer fields when the EF hits a boolean I get: "No coercion operator is defined between types 'System.Int16' and 'System.Boolean'."

The example comes with three contexts, but only one of them, ApplicationDbContext, is referenced in the source code. For that one, I can resolve the issue in OnModelCreating using:
builder
       .Entity<ApplicationUser>()
       .Property(u => u.EmailConfirmed).HasConversion<Int16>()
       ;


The trouble is the other two contexts, e.g. ConfigurationDbContext, are part of IdentityServer4.EntityFramework.DbContexts so the source is not part of the example. I have a number of choices and I need some advice on what direction I should take. I see these as my options:

Replace MySql with SqlServer
Probably the easiest thing to do, and despite my code having components that go back more than 10 years, the effort would not be huge because the database dependent calls were kept seperate from day 1 for this eventuality. But I like MySql.

Extend ConfigurationDbContext via a partial class
This would allow me to add an OnModelCreating method via the partial. But I'm not sure that this is techincally possible since the original definition is not a partial.

Create a new context that inherits from ConfigurationDbContext
This is looking increasingly like the solution of choice although it is a little more effort than either of the previous two.

Pull down a copy of the IdentityServer4 code itself and modify ConfigurationDbContext class
This would give me considerable power to overcome the MySql roadblocks but at the expense of comming off the 'upgrade path' of the Identity Server nuget packages. I would be reluctant to go down that road.

Something else not listed here


So in summary I need advice on how I might add an OnModelCreating method to the IdentitySever ConfigurationDbContext class.

What I have tried:

I have resolved the issue in one of the three places it occurs, then researched and listed possibilities for resolving it in the other two. Now I need advice on the direction to take.
Posted
Updated 22-Apr-19 21:20pm
Comments
Christian Graus 23-Apr-19 3:08am
   
Isn't Identity Server open source? Can't you just compile your own version?
Ger Hayden 23-Apr-19 5:36am
   
Hi Christian, that is one of my choices, but against it is that every time they publish a change, I have to merge it with my fork.
Christian Graus 23-Apr-19 16:55pm
   
Yeah, that does suck. I'd suggest doing a branch with a conditional compile for MySQL and ask them to merge it in
Ger Hayden 24-Apr-19 9:01am
   
I've tried partials and extensions on both the entity and the context, now I'm cloning as per your suggestion. Nothing else was satisfactory.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

(You extend the entity, not the dbContext?)

If you want to treat an "int" as a bool, create a proxy property; keep the ORM mapping simple.

public short MyShort {get;set;}

[NotMapped]
public bool MyBool {get{return (MyShort == 1);} set {MyShort = value ? 1 : 0;}}


Add the code to the entity class; code first or as a partial class.
   
Comments
Ger Hayden 23-Apr-19 5:39am
   
Thanks Gerry, I'll look into it, but I will need to see if the entity definition is part of the Identity Server Nuget packages, if it is, I am reluctant to change it because it puts me out on my own fork of the code. Now, your last thought, adding it as a Partial might be a winner!
Ger Hayden 14-Jun-19 6:18am
   
I ended up using SQL Server for Identity Server, but I subsequently ran into the same issue on one of my own data stores and this solution was just perfect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100