jonathanm
jonathanm
Explore posts from servers
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
Thank guys, but I still can't get it working. That is probably because I still don't understand enough about EF. Will try to understand it first.
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
because I get the error The seed entity for entity type 'List<Post>' cannot be added because no value was provided for the required property 'Capacity'.
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
Do you mean this?
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

var newUser = new User
{
UserName = "JohnDoe",
};

modelBuilder.Entity<User>(u =>
{
u.HasData(newUser);
u.OwnsOne(s => s.Posts).HasData(new Post
{
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
});
});
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

var newUser = new User
{
UserName = "JohnDoe",
};

modelBuilder.Entity<User>(u =>
{
u.HasData(newUser);
u.OwnsOne(s => s.Posts).HasData(new Post
{
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
});
});
}
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
Is this good?
cs public List<Post> Posts { get; set; } = new List<Post>();
cs public List<Post> Posts { get; set; } = new List<Post>();
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
Okay, thank you
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
But this is a One-to-many relation. I don't understand whats wrong with it.
28 replies
CC#
Created by jonathanm on 5/30/2023 in #help
❔ Can't seed one-to-many relation efcore
Oh yeah sorry, forgot to share: The seed entity for entity type 'Post' cannot be added because it has the navigation 'User' set. To seed relationships, add the entity seed to 'Post' and specify the foreign key values {'UserId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
28 replies