首页 > 解决方案 > Xamarin(表单)iOS Entity Framework 迁移问题 - 实体上没有键(它是)错误

问题描述

在我的 DbContext 的构造函数中,对 Database.Migrate 的调用失败告诉我我的实体没有主键(它有,我正在使用 Key 属性)

最初这是当我更新现有应用程序但我删除了数据库并且它仍在发生时。(如果数据库不存在,迁移应该创建数据库)。

这只是 iOS 上的一个问题 - 在 Android 上迁移需要,没问题。

编辑:我已经回到了我知道有效的先前提交(它在应用商店中!并且没有任何问题) - 我现在也得到了那个版本的例外 - 而且,我复制了项目到另一台机器上(使用旧版本的VS),当我在手机上调试时,它也会在那里抛出异常)

配置如下所示:

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

    public string Token { get; set; }

    public string DeviceIdentifier { get; set; }
}

迁移如下(但我认为它实际上并没有像迁移一样,可能是错误的)

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");
    }
}

和:

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");
    }
}

最后:

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");
    }
}

模型生成器(包括密钥!)

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 警告恢复 612, 618 }

标签: c#iosentity-frameworkxamarin

解决方案


问题在于链接设置,它被设置为“全部链接”-当我将其更改为“仅链接框架 SDK”时,它工作正常。


推荐阅读