I have a Comment owned entity type:
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>();
}
And Post's configuration includes:
builder.OwnsMany(x => x.Comments, x => {
x.Property(y => y.Text).IsRequired();
});
The seeding code includes this:
var post = new Post("content");
post.Comments.Add(new Comment("comment1"));
post.Comments.Add(new Comment("comment2"));
await _context.AddAsync(post);
await _context.SaveChangesAsync();
When I use the postgres provider, I can successfully create, seed and edit the database.
When I use the sqlite provider, I can successfully create the database, but when I try to seed it I get this error:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'NOT NULL constraint failed: Comment.Id'.
The docs say that the owned table has an implicit key, which explains the complaint about Comment.Id.
But why does this happen only for sqlite, and how do I fix it?