Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EF core在SQLite中创建自动增量列?_C#_Sqlite_Entity Framework Core_Auto Increment_.net Standard 2.0 - Fatal编程技术网

C# 如何使用EF core在SQLite中创建自动增量列?

C# 如何使用EF core在SQLite中创建自动增量列?,c#,sqlite,entity-framework-core,auto-increment,.net-standard-2.0,C#,Sqlite,Entity Framework Core,Auto Increment,.net Standard 2.0,我在我的应用程序中首先使用Entity Framework Core 2.0作为Sqlite代码。我的模型有一个Primary Key integer类型的实体,它应该根据需要自动递增。但实际上,对于每一行,标识列的值都是0。 请提供帮助,因为我没有找到与此问题相关的帮助材料 这是我的财产,没有任何数据注释 public Int32 No{get;set;} 我使用了fluentapi modelBuilder.Entity<TurnosGeneral>()

我在我的应用程序中首先使用Entity Framework Core 2.0作为Sqlite代码。我的模型有一个Primary Key integer类型的实体,它应该根据需要自动递增。但实际上,对于每一行,标识列的值都是0。 请提供帮助,因为我没有找到与此问题相关的帮助材料

这是我的财产,没有任何数据注释

public Int32 No{get;set;}

我使用了fluentapi

modelBuilder.Entity<TurnosGeneral>()
            .HasKey(c => new { c.No, c.Cod_Turno });
db.SaveChanges()

对于插入的每一行,c.编号为0

我的模型有一个主键整数类型的实体,它应该作为自动增量

问题在于,所讨论的属性不是PK,而是复合PK的一部分,在这种情况下,它不被视为根据约定自动生成的,如EF核心文档的一节所述:

按照惯例,非复合类型为short、int、long或Guid的主键将被设置为在添加时生成值。所有其他属性将在不生成值的情况下进行设置

您需要明确指定:

modelBuilder.Entity<TurnosGeneral>()
    .Property(e => e.No)
    .ValueGeneratedOnAdd();
modelBuilder.Entity()
.属性(e=>e.No)
.ValueGeneratedOnAdd();

更新:以上是适用于大多数数据库的一般方法。但SQLite支持自动增量,因此这不是EF核心的限制。不要使用自动递增或使其成为非复合PK。

看起来像个bug。选中此项:


我的解决方法:手动将DBContext类中Id列上的:.ValueGeneratedNever()更改为.ValueGeneratedOnAdd()

我刚刚使用了一个用于测试的SQLite内存数据库。在我的例子中,我有一个商业类,其主键字段名为
ContactCategoryId

public class ContactCategory
{
    [Required]
    [Display(Name = "Contact category ID")]
    public int ContactCategoryId { get; set; }
我也在使用流畅的方法进行设置:

public void Configure(EntityTypeBuilder<ContactCategory> modelBuilder)
{
    modelBuilder.ToTable("CONTACT_CATEGORY", _schema);

    modelBuilder.HasKey(x => x.ContactCategoryId);

    modelBuilder.Property(x => x.ContactCategoryId)
        .HasColumnName(@"ContactCategoryID")
        //.HasColumnType("int") Weirdly this was upsetting SQLite
        .IsRequired()
        .ValueGeneratedOnAdd()
        ;
public void配置(EntityTypeBuilder modelBuilder)
{
ToTable(“联系类别”,模式);
HasKey(x=>x.ContactCategoryId);
属性(x=>x.ContactCategoryId)
.HasColumnName(@“ContactCategoryID”)
//.HasColumnType(“int”)奇怪的是,这让SQLite心烦意乱
.IsRequired()
.ValueGeneratedOnAdd()
;

注释掉
.HasColumnType(“int”)
的行为我修复了错误。

我已经尝试过了,并且总是得到一个错误“SqliteException:SQLite error 19:'非空约束失败:”这意味着它不会在添加时自动生成预期的用户生成的值。毫无疑问,这在EF 6中适用于sql server。在EF Core 2.0.2中也适用于sql server。在文档中找不到SQLite特定的要求。好的,我已经安装了[SQLite EF Core Database Provider(),并完成了此迁移(注意Sqlite:Autoincrement):
migrationBuilder.CreateTable(名称:“TurnosGeneral”,列:table=>new{No=table.Column(可空:false)。注释(“Sqlite:Autoincrement”,true),Cod_Turno=table.Column(可空:false)},约束:table=>{table.PrimaryKey(“pkurTurnCurrand”,x= >新{x.NO,x.cord-Turn});。但是您是正确的,SQLite提供程序迁移生成器中似乎有一个错误,没有考虑到注释。考虑在其问题跟踪器中发布一个问题。也不工作已经尝试。顺便说一句,谢谢你的评论。似乎我必须在github上为SQLite的EF Core创建新版本。对我来说,它只有在我完全删除entity.Property(e=>e.Id)。HasColumnName(“Id”);
public void Configure(EntityTypeBuilder<ContactCategory> modelBuilder)
{
    modelBuilder.ToTable("CONTACT_CATEGORY", _schema);

    modelBuilder.HasKey(x => x.ContactCategoryId);

    modelBuilder.Property(x => x.ContactCategoryId)
        .HasColumnName(@"ContactCategoryID")
        //.HasColumnType("int") Weirdly this was upsetting SQLite
        .IsRequired()
        .ValueGeneratedOnAdd()
        ;