c# - 删除 EF Core 1.0 RC2(以前的 EF 7 RC2)中的自动增量

标签 c# sqlite entity-framework-core ef-fluent-api

在 Entity Framework Core 1.0 RC2(以前的 Entity Framework 7 RC2)中,默认情况下,所有整数主键都是自增字段。我尝试了一切来删除它。从使用数据注释到流畅的 API,没有任何效果。

使用数据注释:

[Key, Column(Order = 1, TypeName = "INT"), DatabaseGenerated(DatabaseGeneratedOption.None)]

使用流畅的 API:

modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("DatabaseGenerated", DatabaseGeneratedOption.None);

//OR use the following
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("DatabaseGenerated", 0);

//OR use the following
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("Sqlite:Autoincrement", false);

没有任何效果:(

你能帮帮我吗?

已更新

根据要求,这是我在运行 add-migration LocalDB_v1 后得到的表脚本

migrationBuilder.CreateTable(
            name: "tblProduct",
            columns: table => new
            {
                ProdId = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Name = table.Column<string>(nullable: true),
                Description = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_tblProduct", x => x.ProdId);
            });
...
...
...

最佳答案

在 EF 核心中,key and property are configured separately .

指定 key :

modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId);

配置属性不自增:

modelBuilder.Entity<tblProduct>().Property(t => t.ProdId).ValueGeneratedNever();

关于c# - 删除 EF Core 1.0 RC2(以前的 EF 7 RC2)中的自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41908391/

相关文章:

c# - SQL参数: Argument is not used in format string from Resharper

c# - 我可以为使用套接字而不是 “Windows store app library”的Windows应用商店编写一个库吗?

android - 尝试打开sqlite数据库后,我的android应用程序崩溃

php - 将 PDO::FETCH_ASSOC 转换为 SQLite

c# - EF Core 是否支持通用的抽象基础实体?

c# - "select"实体在运行时包含导航属性

c# - 如何删除 UWP 中 ListViewItem 的破损背景?

c# - 与串行端口 “Verifone VX520” 通信

c# - EntityFramework 按 linq 查询分组

android - Unity Android 构建为 Application.streamingAssetsPath 中的文件提供 DirectoryNotFoundException