我有一个属于评论
的实体类型:
public class Comment { // owned entity type
public Comment(string text) { Text = text; }
public string Text { get; private set; }
}
public class Post {
public Post(string content) { Content = content; }
public long Id { get; private set; }
public string Content { get; private set; }
public ICollection<Comment> Comments { get; private set; } = new HashSet<Comment>();
}
而 Post
的配置包括:
builder.OwnsMany(x => x.Comments, x => {
x.Property(y => y.Text).IsRequired();
});
播种代码包括以下内容:
var post = new Post("content");
post.Comments.Add(new Comment("comment1"));
post.Comments.Add(new Comment("comment2"));
await _context.AddAsync(post);
await _context.SaveChangesAsync();
当我使用postgres提供程序时,我可以成功地创建、填充和编辑数据库。
当我使用sqlite提供程序时,我可以成功地创建数据库,但是当我尝试填充它时,我会收到以下错误信息:
Microsoft.EntityFrameworkCore.DbUpdateException: 更新条目时发生错误。有关详细信息,请参见内部异常。
---> Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite 错误 19:'NOT NULL constraint failed: Comment.Id'。 文档中提到所拥有的表具有隐式键,这解释了对Comment.Id的投诉。
但为什么只有在sqlite中会出现此问题,以及如何解决它?