Closed laball closed 2 years ago
Hi,
Is any update for this? I got same one.
Thanks
Hi, Yes I have the same problem.
@laball This looks like an issue with the MySQL provider; please file an issue with them. Also, you might want to try the Pomelo open-source provider--some people have had more luck with it than with the official provider.
I had the same issue and you can try the Pomelo as was suggested by @ajcvickers or you can use value conversions:
entity.Property(o => o.[PropertyName]).HasConversion<int<a>>();
I had problems with some DSets for my custom tables as well as with the IdentityUser.
builder.Entity<ApplicationUser>(i => { i.Property(o => o.EmailConfirmed).HasConversion<int>(); i.Property(o => o.LockoutEnabled).HasConversion<int>(); i.Property(o => o.PhoneNumberConfirmed).HasConversion<int>(); i.Property(o => o.TwoFactorEnabled).HasConversion<int>(); });
Note: ApplicationUser
is inherited from the IdentityUser
For me the biggest problem with Pomelo provider is that it does not work very well with async methods
I had the same problem. Also had other unhandled exceptions about converting between Int16 and Boolean whenever I used any bool property on my code-first models. I swapped from MySql.Data.EntityFrameworkCore to Pomelo.EntityFrameworkCore.MySql and now everything works fine.
I fixed it by add a Converter like this:
using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Laball.Core.Common
{
public class BoolToIntConverter : ValueConverter<bool, int>
{
public BoolToIntConverter([CanBeNull] ConverterMappingHints mappingHints = null)
: base(
v => Convert.ToInt32(v),
v => Convert.ToBoolean(v),
mappingHints)
{
}
public static ValueConverterInfo DefaultInfo { get; }
= new ValueConverterInfo(typeof(bool), typeof(int), i => new BoolToIntConverter(i.MappingHints));
}
}
and add it to the DbContext like this:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach (var entityType in builder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(bool))
{
property.SetValueConverter(new BoolToIntConverter());
}
}
}
}
and it works fine. I forgot to post here.
@rexhong19871011 @Alex-Schelkov @ajcvickers @karac38 @bigvigg
EF Core 2.1 has Built-in converters: for example BoolToZeroOneConverter
It works for me:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity< Role >()
.Property(r => r.Enabled)
.HasConversion(new BoolToZeroOneConverter<Int16>());
}
Provider: MySql.Data.EntityFrameworkCore
@ceshdelgado I placed your sample code in my OnModelCreating but "....Entity<Role>" is throwing an assembly reference error .
Im using EF 2.1.8
Note BoolToZeroOneConverter requires: using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
I added the following code which automatically applies the BoolToZeroOneConverter<short>
to every boolean property without having to do it manually for each property
Here is the DbContext class, to show how I am overriding the OnModelCreating
public class ApplicationDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Iterate over every DbSet<> found in the current DbContext
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
// Iterate over each property found on the Entity class
foreach (IMutableProperty property in entityType.GetProperties())
{
if (property.PropertyInfo == null)
{
continue;
}
if (property.IsPrimaryKey() && IsPrimaryKey(property.PropertyInfo))
{
// At this point we know that the property is a primary key
// let's set it to AutoIncrement on insert.
modelBuilder.Entity(entityType.ClrType)
.Property(property.Name)
.ValueGeneratedOnAdd()
.Metadata.BeforeSaveBehavior = PropertySaveBehavior.Ignore;
}
else if (property.PropertyInfo.PropertyType.IsBoolean())
{
// Since MySQL stores bool as tinyint, let's add a converter so the tinyint is treated as boolean
modelBuilder.Entity(entityType.ClrType)
.Property(property.Name)
.HasConversion(new BoolToZeroOneConverter<short>());
}
}
};
}
private static bool IsPrimaryKey(PropertyInfo property)
{
var identityTypes = new List<Type> {
typeof(short),
typeof(int),
typeof(long)
};
return property.Name.Equals("Id", StringComparison.CurrentCultureIgnoreCase) && identityTypes.Contains(property.PropertyType);
}
}
Here are the type extensions
public static class TypeExtensions
{
public static bool IsBoolean(this Type type)
{
Type t = Nullable.GetUnderlyingType(type) ?? type;
return t == typeof(bool);
}
public static bool IsTrueEnum(this Type type)
{
Type t = Nullable.GetUnderlyingType(type) ?? type;
return t.IsEnum;
}
}
If others still run into this still just set the TypeName to bit on the Column attribute to your entity.
Example:
[Column(TypeName = "bit")] public Nullable<bool> MyColumn { get; set; }
I still have problems with this same error, but it occurs when using .Select() when querying with a bool in a navigation object.
Context.Users.Where(x => x.Id == 10)
.Select(s => new UserVM
{
Name = s.Name
TestVM = new TestVM
{
Active = s.TestNavigation.Active
}
}) .FirstOrDefault();
EF Core version: 2.2.6 Database Provider: MySql.Data Version 8.0.17 MySql.Data.EntityFrameworkCore Version 8.0.17
@CrestApps, your solution solution worked for me. My project is Core 2.2 and importing packages below
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6" PrivateAssets="All" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.19" />
While I use EF Core 2.1.4 with MySQL, I will got an error while use bool field in entity,
Steps to reproduce
I was use EF Core with ABP framework,while I use bool filed in my entity, there always got a error "No coercion operator is defined between types 'System.Int16' and 'System.Boolean'"
But,when I use some code like this ,it gone:
my entity and use code:
Further technical details
EF Core version: 2.1.4 Database Provider: MySql.Data Version 8.0.13 MySql.Data.EntityFrameworkCore Version 8.0.13 Operating system: Windows 7 professional