Entity Framework MySQL tinyint(1) System.Boolean.Parse FormatException

后端 未结 3 438
梦如初夏
梦如初夏 2021-01-12 01:18

I\'m using EntityFramework 6 in my C# model-first project which using a MySQL database. Everything was fine and I could ge

相关标签:
3条回答
  • 2021-01-12 02:04

    This can also be achieved using the Column data attribute within the Entity.

    [Column("Active", TypeName = "bit")]
    [DefaultValue(false)]
    public bool Active { get; set; }
    
    0 讨论(0)
  • 2021-01-12 02:06

    If you are doing this DB first, simply change the TINYINT(1) types to BIT(1), assuming you really want a Boolean. You may have to update the default values also (to bit syntax such as b'0'). EF will still translate these to Boolean values in your entities.

    0 讨论(0)
  • 2021-01-12 02:12

    Configure the datatype on a specific Entity:

    modelBuilder.Entity<User>()
                      .Property(p => p.Active)
                      .HasColumnType("bit");
    

    or general:

    modelBuilder.Properties()
                .Where(x => x.PropertyType == typeof(bool))
                .Configure(x => x.HasColumnType("bit"));
    
    0 讨论(0)
提交回复
热议问题