繁体   English   中英

如何投射Tinyint(1)MySQL EF Core

[英]How to cast Tinyint(1) MySQL EF Core

我试图让EF核心首先使用DB和Pomelo来构建一个MySQL数据库但由于tinyint(1)导致bool转换失败问题而无法运行简单的查询。 尝试使用这两个连接字符串作为脚手架:

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

对于表中的tinyint(1)列,我们在模型中使用bool属性并手动管理它。 有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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