c# - 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 }
解决方案
问题在于链接设置,它被设置为“全部链接”-当我将其更改为“仅链接框架 SDK”时,它工作正常。
推荐阅读
- r - 如何将链接指向 .Md 文件中的另一个选项卡
- ruby-on-rails - 如何在特定控制器上渲染部分
- html - 我们如何使用位置 :sticky; 表中的属性粘贴标题部分?
- javascript - 电子生成器。仅在构建 Mac 应用程序时如何挂钩 afterSign
- c++ - std::stable_partition() 和 std::partition() 有什么区别?
- drupal-8 - Drupal 8 中的权限表在哪里?
- javascript - 无法执行 javascript onclick 事件
- cmake - 链接由依赖库链接的第三方库,使用 Cmake
- opencv - 如何从一本书的页面中提取一堆图像?
- joomla - 带有插入图像按钮的 Joomla 4 alpha 和模态窗口