【发布时间】:2022-02-17 15:16:10
【问题描述】:
我有一个错误: Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: '数据库操作预计会影响 1 行,但实际上影响了 0 行;自加载实体后,数据可能已被修改或删除。
当我尝试使用实体框架向数据库中的帖子添加评论并保存更改时。我不知道为什么。我做错了什么?
这里我有错误:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ApplicationDbContext _applicationDbContext;
public ValuesController(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}
[HttpGet]
public IActionResult Get()
{
var post = _applicationDbContext.Posts.First();
post.AddComment(Guid.NewGuid(), "aaa", "bbb");
_applicationDbContext.SaveChanges(); // here error
return Ok();
}
}
实体框架配置 - 这里可能有问题?:
public class PostConfiguration : IEntityTypeConfiguration<Post>
{
public void Configure(EntityTypeBuilder<Post> builder)
{
builder.ToTable("Posts");
builder.HasKey(x => x.PostId);
builder.HasMany(x => x.Comments).WithOne().HasForeignKey("PostId");
}
}
public class CommentConfiguration : IEntityTypeConfiguration<Comment>
{
public void Configure(EntityTypeBuilder<Comment> builder)
{
builder.ToTable("Comments");
builder.HasKey(x => x.CommentId);
builder.Property(x => x.CommentStatus)
.HasColumnName("CommentStatusId")
.HasConversion(new EnumToNumberConverter<CommentStatus, int>());
}
}
public class ApplicationDbContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public ApplicationDbContext(
DbContextOptions options) : base(options)
{
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
var result = await base.SaveChangesAsync(cancellationToken);
return result;
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(builder);
}
}
这是我的帖子类:
public class Post
{
public Guid PostId { get; private set; }
public string Title { get; private set; }
public string Content { get; private set; }
public List<Comment> Comments { get; private set; } = new List<Comment>();
public Post(Guid postId, string title, string content)
{
PostId = postId;
Title = title;
Content = content;
}
public void AddComment(Guid commentId, string author, string content)
{
var comment = new Comment(commentId, author, content);
Comments.Add(comment);
}
}
这是我的评论类:
public class Comment
{
public Guid CommentId { get; private set; }
public string Author { get; private set; }
public string Content { get; private set; }
public CommentStatus CommentStatus { get; private set; }
public Comment(Guid commentId, string author, string content)
{
CommentId = commentId;
Author = author;
Content = content;
CommentStatus = CommentStatus.New;
}
}
这是我的 CommentStatus 类:
public enum CommentStatus
{
New = 1,
Accepted = 2,
Rejected = 3
}
在 Program.cs 我有:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
【问题讨论】:
-
好的,我知道了,我必须添加:builder.Property(x => x.Postd).ValueGeneratedNever(); builder.Property(x => x.CommentId).ValueGeneratedNever();
-
请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一,如果有的话。如果现有答案尚未涵盖您的解决方案,您还可以创建自己的答案,甚至接受它。比较stackoverflow.com/help/self-answer
标签: c# asp.net .net entity-framework