Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/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# 尝试插入数据时实体框架核心中的一对多关系出错_C#_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 尝试插入数据时实体框架核心中的一对多关系出错

C# 尝试插入数据时实体框架核心中的一对多关系出错,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我正在尝试向一对多关系表添加数据。但也有例外 有两个表格: 职位 附件: 帖子有很多附件,但一个附件有一个唯一的帖子。我将尝试: 将记录添加到Post表,然后 使用该post-Id。更新附件表 下面是抛出的异常 InvalidOperationException:实体类型“Posts”上的属性“Id”具有临时值。请显式设置永久值,或确保将数据库配置为生成此属性的值 在Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPrepare

我正在尝试向一对多关系表添加数据。但也有例外

有两个表格:

  • 职位
  • 附件:
  • 帖子有很多附件,但一个附件有一个唯一的帖子。我将尝试:

  • 将记录添加到
    Post
    表,然后
  • 使用该post-Id。更新
    附件
  • 下面是抛出的异常

    InvalidOperationException:实体类型“Posts”上的属性“Id”具有临时值。请显式设置永久值,或确保将数据库配置为生成此属性的值

    在Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.Validate(ModificationCommand ModificationCommand)
    位于Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.d_u8.MoveNext()
    在Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(元组<代码>2个参数)
    在Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state,Func3操作,Func
    3验证成功)
    在Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IEExecutionStrategy strategy,TState state,Func
    2操作) 在Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable
    1 commandBatches,IRelationalConnection)
    在Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList
    1条目)中 位于Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave) 位于Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(布尔接受更改成功) 在Microsoft.EntityFrameworkCore.DbContext.SaveChanges(布尔接受更改成功) 在Microsoft.EntityFrameworkCore.DbContext.SaveChanges()中 在C:\Users\kokuadmin\source\repos\GradChat\GradChat.Data.Repo.PostRepository.PostRepo.AddPost(Posts post)中的GradChat.Data.Repo\PostRepository\PostRepo.cs:第27行

    这里是my
    OnModelCreating()

    模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
    {    
    modelBuilder.Entity()
    .HasOne(p=>p.User)
    .有许多(c=>c.职位)
    .HasForeignKey(p=>p.Id);
    modelBuilder.Entity()
    .HasOne(p=>p.Posts)
    .WithMany(c=>c.附件)
    .HasForeignKey(p=>p.Id);
    }    
    }
    
    以下是两个实体类:

    public class Attachment
    {
        public int Id { get; set; }    
        public string FileName { get; set; }    
        public string FileTye { get; set; }    
        public virtual Posts Posts { get; set; }    
        public int PostId { get; set; }    
    }
    
    public class Posts
    {     
        public int Id { get; set; }    
        public string Title { get; set; }    
        public string Content { get; set; }    
        public virtual User User { get; set; }    
        public int UserId { get; set; }    
        public virtual ICollection<Attachment> Attachment { get; set; }    
    }
    
    公共类附件
    {
    公共int Id{get;set;}
    公共字符串文件名{get;set;}
    公共字符串FileTye{get;set;}
    公共虚拟帖子{get;set;}
    公共int PostId{get;set;}
    }
    公营职位
    {     
    公共int Id{get;set;}
    公共字符串标题{get;set;}
    公共字符串内容{get;set;}
    公共虚拟用户用户{get;set;}
    public int UserId{get;set;}
    公共虚拟ICollection附件{get;set;}
    }
    
    我正在使用Microsoft.EntityFramework.Core.SqlServer(2.0.1)

    我修复了这个问题

    将OnModelCreating()上的
    更改为如下所示。谢谢你的帮助。只需将,
    .HasForeignKey(p=>p.Id)
    更改为
    .HasForeignKey(p=>p.UserId)

    modelBuilder.Entity()
    .HasOne(p=>p.User)
    .有许多(c=>c.职位)
    .HasForeignKey(p=>p.UserId);
    modelBuilder.Entity()
    .HasOne(p=>p.Posts)
    .有许多(c=>c.附件);
    
    在post模型中,您在哪里分配了附件实体的外键?类似于post模型中的
    public int AttachmentId{get;set;}
    关系是一篇文章有很多附件,但一个附件有唯一的postVisite,这对您有帮助删除
    public int PostId{get;set;}
    来自
    附件
    型号bcoz它将由EF Core自动创建很高兴听到您已经解决了问题:)谢谢您的帮助。:)
    public class Attachment
    {
        public int Id { get; set; }    
        public string FileName { get; set; }    
        public string FileTye { get; set; }    
        public virtual Posts Posts { get; set; }    
        public int PostId { get; set; }    
    }
    
    public class Posts
    {     
        public int Id { get; set; }    
        public string Title { get; set; }    
        public string Content { get; set; }    
        public virtual User User { get; set; }    
        public int UserId { get; set; }    
        public virtual ICollection<Attachment> Attachment { get; set; }    
    }
    
     modelBuilder.Entity<Posts>()
              .HasOne(p => p.User)
              .WithMany(c => c.Posts)
              .HasForeignKey(p => p.UserId);
    
          modelBuilder.Entity<Attachment>()
            .HasOne(p => p.Posts)
            .WithMany(c => c.Attachment);