dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.1k stars 9.91k forks source link

EntityFramework Migration for ASP.Net Core Identity creates Key without key length #28055

Open jonasarcangel opened 3 years ago

jonasarcangel commented 3 years ago

Using ASP.Net Core Identity and EntityFramework, the migration code generated created key (for tables like ApplicationUser, from IdentityUser) with Id and no key length.

Getting an error when running with MySql:

MySqlConnector.MySqlException (0x80004005): BLOB/TEXT column 'Id' used in key specification without a key length

For example:

    modelBuilder.Entity("MyProject.ApplicationUser", b =>
        {
            b.Property<string>("Id")
                .HasColumnType("TEXT");
rezathecoder commented 3 years ago

Try changing your migration file manually:

migrationBuilder.CreateTable( name: "Users", columns: table => new { Id = table.Column<string>(nullable: false),

Change it to

Id = table.Column<string>(nullable: false, maxLength: 450)

mkArtakMSFT commented 3 years ago

@blowdart FYI

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

mahomedalid commented 2 years ago

@jonasarcangel I created a sample template to solve this problem https://github.com/mahomedalid/blazor-auth-mysql you probably can use this https://github.com/mahomedalid/blazor-auth-mysql/blob/main/Data/Migrations/00000000000000_CreateIdentitySchema.cs

mahomedalid commented 2 years ago

I would like to work in this issue, I'm working in a PR to add a new option, with the following characteristics:

I think this should be important, because it helps new developers into Blazor to setup a quick boilerplate/project to use with some of the most open-source tools. I can see this as a great help and introduction for LAMP (Linux/Apache/MySQL/PHP) developers and will likely attract new users. I also see this as a start to add support for other databases like PostgreSQL.

@mkArtakMSFT @msftbot Let me know what you think.

christiannagel commented 3 months ago

I had this issue creating a .NET 8 Blazor identity project, configured it with .NET Aspire, and connected it with the .NET Aspire MySQL Entity Framework component.

I tried to overwrite the model definition with ApplicationDbContext, and re-generated the migration code. Migrations still failed.

@mahomedalid - thanks for your template updates! Copying the migrations files from your repo fixed it for me. Looking at the differences, it looks like changing maxlength alone is not enough. Your templates also changed the type from TEXT to varchar(256).

@mkArtakMSFT - I think after the release of .NET Aspire, more people will have this issue, and the label affected-few probably no longer applies.