evandro10
evandro10

Reputation: 132

How to make MySql and EF Core to map tinyint or bit to boolean?

I have a .NET Core project where I use EF Core to scaffold our MySql database (db first approach). I'm not able to make EF Core to map tinyint or bit as bool. It maps tinyint to byte and bit to short. Does anyone have any ideas?

Upvotes: 4

Views: 3979

Answers (2)

Bradley Grainger
Bradley Grainger

Reputation: 28162

Make sure you're using Pomelo.EntityFrameworkCore.MySql as your MySQL EF Core provider. (It already supports EF Core 3.0 and is more reliable than Oracle's package.)

Since 3.0, mapping TINYINT(1) to System.Boolean is the default (reference), as long as your connection string includes TreatTinyAsBoolean=True (which is the default setting if not explicitly set).

Upvotes: 4

Eranga Gamagedara
Eranga Gamagedara

Reputation: 536

You can write meta classes for generated partial classes from entity framework.

lets assume you have a class call Status like bellow

public class Status{
public int Value{get;set;}
}

you can write meta class for it like bellow.

 [ModelMetadataType(typeof(TblUserMetaData))]
        public partial class Status
        {
        }

        public class TblUserMetaData
        {
           public int Value{get;set;}
           public bool ValueBool{get{ 
            //gett Value variable by converting boolean
            };
         set{
             //set Value variable by converting int
           };
          }
        }
    }
  • you need to place meta class in same namespace

Upvotes: 3

Related Questions