如何转换Tinyint(1)MySQL EF Core

时间:2019-06-19 03:30:48

标签: c# mysql entity-framework

我试图让EF内核先使用DB和Pomelo来搭建MySQL数据库,但是由于tinyint(1)会导致转换失败,因此无法运行简单查询。尝试将这两个连接字符串都用于脚手架:

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

如果任何人都可以指出出什么毛病了,那就太好了

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}");
        }
    }

这是脚手架模型:

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; }
    }
}

1 个答案:

答案 0 :(得分:0)

对于表中的tinyint(1)列,我们在模型中使用了bool属性,并对其进行了手动管理。可行