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

Add-Migration generate migration with missing "using" statement when a property is an enum defined in another namespace and using a conversion. #32552

Closed
Dunge opened this issue Dec 7, 2023 · 1 comment · Fixed by #32619
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug

Comments

@Dunge
Copy link

Dunge commented Dec 7, 2023

If your model has a property of type enum and that enum is defined in another namespace than your model. When generating a migration, by default the snapshot and migration.designer.cs file will use "int" as the variable type and it works.

But when you add a HasConversion() statement (even if empty), the generated snapshot and designer.cs files of the migration will use the enum type. Like so:

                    b.Property<SomeEnum>("Type")
                        .HasColumnType("int");

This results in a project that does not build, because the generated migration files are missing the "using" statement of the enumeration namespace on top of their files.

Include your code

Full reproducible project (just replace the connection string and table name):

using EfCoreMigrationEnum.Enums;
using Microsoft.EntityFrameworkCore;

namespace EfCoreMigrationEnum
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            using (var context = new BloggingContext())
            {
                var posts = await context.Posts.Where(t => t.Type == SomeEnum.Value2).ToListAsync();
            }
        }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlServer(***);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>(entity =>
            {
                entity.ToTable("Post");
                entity.HasKey(x => x.ID);
                entity.Property(x => x.Type).HasConversion(e => e, e => e);
            });
        }
    }

    public class Post
    {
        public int ID { get; set; }
        public SomeEnum Type { get; set; }
    }
}

namespace EfCoreMigrationEnum.Enums
{
    public enum SomeEnum
    {
        Value1,
        Value2,
        Value3
    }
}

Include verbose output

PM> Add-Migration MInitial -Context BloggingContext
Build started...
Build succeeded.

Build afterward:

1>C:\Users\***\source\repos\EfCoreMigrationEnum\Migrations\BloggingContextModelSnapshot.cs(32,32,32,40): error CS0246: The type or namespace name 'SomeEnum' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\***\source\repos\EfCoreMigrationEnum\Migrations\20231207203356_MInitial.Designer.cs(35,32,35,40): error CS0246: The type or namespace name 'SomeEnum' could not be found (are you missing a using directive or an assembly reference?)

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11 x64
IDE: Visual Studio 2022 17.9

@Dunge Dunge changed the title Add-Migration generate migration with missing "using" statement when a property is an enum define in another namespace and using a conversion. Add-Migration generate migration with missing "using" statement when a property is an enum defined in another namespace and using a conversion. Dec 7, 2023
@AndriySvyryd AndriySvyryd added type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. area-migrations labels Dec 15, 2023
@AndriySvyryd AndriySvyryd added this to the 9.0.0 milestone Dec 15, 2023
@Dunge
Copy link
Author

Dunge commented Dec 15, 2023

Amazing, thank you so much for looking at and fixing this issue!

I see the 9.0.0 milestone tag, but also that it got merged in main. Does that mean it will be patched in the next 8 minor release?

@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants