简体   繁体   中英

How to cast Tinyint(1) MySQL EF Core

I have tried to get EF core to scaffold a MySQL DB using DB first and Pomelo but can't run simple queries because of the tinyint(1) to bool cast failure issue. Tried using both of these connection strings for scaffolds:

Scaffold-DbContext "Server=ip.address.here;Database=DBNameUsername=UnameHere;Password=PasswordHereTreatTinyAsBoolean=false;" Pomelo.EntityFrameworkCore.MySQL -OutputDir Models -force

Scaffold-DbContext "Server=ip.address.here;Database=DBNameUsername=UnameHere;Password=PasswordHereTreatTinyAsBoolean=true;" Pomelo.EntityFrameworkCore.MySQL -OutputDir Models -force

If anyone could point out what's wrong that'd be great

public static Customer GetCustomerById(int id)
    {
        try
        {
            Customer customer = new Customer();

            using (Context db = new Context())
            {
                customer = db.Customer.Single(c => c.CustomerId == id);
            }

            return customer;
        }
        catch (Exception err)
        {
            // always errors on cast conversion failure here
            Console.WriteLine("error: " + err);
            throw new Exception($"couldn't find the customer with CustomerId: {id}");
        }
    }

here is the scaffold-ed model:

using System;
using System.Collections.Generic;

namespace Scheduler.Data.Models
{
    public partial class Customer
    {
        public Customer()
        {
            Appointment = new HashSet<Appointment>();
        }

        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
        public int AddressId { get; set; }
        public sbyte Active { get; set; }
        public DateTime CreateDate { get; set; }
        public string CreatedBy { get; set; }
        public DateTime LastUpdate { get; set; }
        public string LastUpdateBy { get; set; }

        public virtual Address Address { get; set; }
        public virtual ICollection<Appointment> Appointment { get; set; }
    }
}

for tinyint(1) column in table we used bool property in model and managed it manually. It worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM