Xamarin (forms) iOS Entity Framework migration issue – no key on entity (it is) error

In the constructor of my DbContext, the call to Database.Migrate is failing telling me that my entity doesn't have a primary key (it does, I am using the Key attribute)

Initially this was when I was updating an existing app but I have deleted the database and its still happening. (the Migrate should create the DB if its not there).

This is only a problem on iOS - on Android the migration takes, no problem.

The config looks like this:

public class Config
{
    [Key]
    public int ConfigID { get; set; }

    public string Token { get; set; }

    public string DeviceIdentifier { get; set; }
}

The migrations are as follows (but I don't think it actually gets as far as the migrations, could be wrong)

public partial class InitialMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Configs",
            columns: table => new
            {
                ConfigID = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Token = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Configs", x => x.ConfigID);
            });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Configs");
    }
}

And:

public partial class DiagnosticsAdded : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Diagnostics",
            columns: table => new
            {
                DiagnosticID = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Key = table.Column<string>(nullable: true),
                Value = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Diagnostics", x => x.DiagnosticID);
            });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Diagnostics");
    }
}

Finally:

public partial class DeviceIdentifierAdded : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<string>(
            name: "DeviceIdentifier",
            table: "Configs",
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "DeviceIdentifier",
            table: "Configs");
    }
}

The Model Builder (includes the Key!)

protected override void BuildModel(ModelBuilder modelBuilder)
    {
#pragma warning disable 612, 618
        modelBuilder
            .HasAnnotation("ProductVersion", "3.1.16");

        modelBuilder.Entity("Mobile.Models.Config", b =>
            {
                b.Property<int>("ConfigID")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("INTEGER");

                b.Property<string>("DeviceIdentifier")
                    .HasColumnType("TEXT");

                b.Property<string>("Token")
                    .HasColumnType("TEXT");

                b.HasKey("ConfigID");
                
                b.ToTable("Configs");
            });

        modelBuilder.Entity("Mobile.Models.Diagnostic", b =>
            {
                b.Property<int>("DiagnosticID")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("INTEGER");

                b.Property<string>("Key")
                    .HasColumnType("TEXT");

                b.Property<string>("Value")
                    .HasColumnType("TEXT");

                b.HasKey("DiagnosticID");

                b.ToTable("Diagnostics");
            });

#pragma warning restore 612, 618 }



Read more here: https://stackoverflow.com/questions/68486581/xamarin-forms-ios-entity-framework-migration-issue-no-key-on-entity-it-is

Content Attribution

This content was originally published by Ben Emanuel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: