Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolding doesn't include tinyint(3) unsigned column in the map #655

Closed
AccessViolator opened this issue Jul 18, 2018 · 5 comments
Closed
Assignees
Milestone

Comments

@AccessViolator
Copy link

Steps to reproduce

Have a table with tinyint(3) unsigned column, run scaffolding.

The issue

The entity is generated correctly and includes property of type byte, however the property is missing from the entity's map in DbContext.OnModelCreating()

Further technical details

MySQL version: 8.0.11
Operating system: Win 7 x64
Pomelo.EntityFrameworkCore.MySql version: 2.1.1

@mguinness
Copy link
Collaborator

Byte maps to tinyint unsigned. Were you expecting HasColumnType with display width?

@AccessViolator
Copy link
Author

I was expecting .Property() entry with or without HasColumnType().
Are you saying it's not there because tinyint(3) unsigned is the default mapping for a byte?

@mguinness
Copy link
Collaborator

It appears that this is caused by the fix for #394 which strips out the display width for unsigned integers.

@lauxjpn
Copy link
Collaborator

lauxjpn commented Nov 4, 2019

This was very likely fixed by #896. I will check this later at some point.

@lauxjpn lauxjpn self-assigned this Nov 4, 2019
@lauxjpn
Copy link
Collaborator

lauxjpn commented Nov 7, 2019

I can confirm that this is fixed now in 3.0.0.

I ran the following SQL:

CREATE TABLE `Issue655`.`IceCream` (
  `Id` INT NOT NULL AUTO_INCREMENT,
  `WeightInGrams` TINYINT(3) UNSIGNED NOT NULL,
  PRIMARY KEY (`Id`)
);

Then i scaffolded the database:

dotnet ef dbcontext scaffold "server=127.0.0.1;user id=root;password=;port=3306;database=Issue655" Pomelo.EntityFrameworkCore.MySql -c Context

The following classes were generated:

public partial class IceCream
{
    public int Id { get; set; }
    public byte WeightInGrams { get; set; } // <-- correct CLR type
}

public partial class Context : DbContext
{
    public Context()
    {
    }

    public Context(DbContextOptions<Context> options)
        : base(options)
    {
    }

    public virtual DbSet<IceCream> IceCream { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseMySql(
"server=127.0.0.1;user id=root;password=;port=3306;database=Issue655");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IceCream>(entity =>
        {
            entity.Property(e => e.Id)
                .HasColumnType("int(11)");
            entity.Property(e => e.WeightInGrams)
                .HasColumnType("tinyint(3) unsigned"); // <-- correct MySQL type
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

Finally, the following test executed successfully:

internal class Program
{
    private static void Main()
    {
        using var context = new Context();

        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        context.IceCream.Add(new IceCream { WeightInGrams = 68 });
        context.SaveChanges();

        var iceCream = context.IceCream.Single();

        Debug.Assert(iceCream.WeightInGrams is byte);
        Debug.Assert(iceCream.WeightInGrams == 68);
    }
}

@lauxjpn lauxjpn added this to the 3.0.0 milestone Nov 7, 2019
@lauxjpn lauxjpn closed this as completed Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants