C
C#2y ago
jonathanm

❔ Can't seed one-to-many relation efcore

Hello, I am stuck on a error while trying to seed a database with Efcore. DataContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

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

modelBuilder.Entity<User>().HasData(newUser);

var newPost = new Post
{
Id = "1",
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
User = newUser
};

modelBuilder.Entity<Post>().HasData(newPost);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

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

modelBuilder.Entity<User>().HasData(newUser);

var newPost = new Post
{
Id = "1",
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
User = newUser
};

modelBuilder.Entity<Post>().HasData(newPost);
}
User.cs
public class User : IdentityUser
{
public Uri? ProfilePicture { get; set; }
public List<Post> Posts { get; set; } = new();
}
public class User : IdentityUser
{
public Uri? ProfilePicture { get; set; }
public List<Post> Posts { get; set; } = new();
}
Post.cs
using System.ComponentModel.DataAnnotations;

namespace Tweeter.Shared.Models;

public class Post
{
public String Id { get; set; }
[Required, MaxLength(280)]
public string Content { get; set; } = "";
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string UserId { get; set; } = "";
public User User { get; set; } = new();
}
using System.ComponentModel.DataAnnotations;

namespace Tweeter.Shared.Models;

public class Post
{
public String Id { get; set; }
[Required, MaxLength(280)]
public string Content { get; set; } = "";
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string UserId { get; set; } = "";
public User User { get; set; } = new();
}
I have tried to put the Post inside of .OwnsMany, but that didn't work. How can I fix this?
18 Replies
Nergy101
Nergy1012y ago
It's never just "a error", Its always important What error. Could you share it in full?
jonathanm
jonathanmOP2y ago
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.
Patrick
Patrick2y ago
if you're trying to use skip navigations you cannot configure it as such. if you want to use FKs like that, you need to declare a join table.
Patrick
Patrick2y ago
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
jonathanm
jonathanmOP2y ago
But this is a One-to-many relation. I don't understand whats wrong with it.
Patrick
Patrick2y ago
= new() you also really shouldn't do this on your nav props.
Nergy101
Nergy1012y ago
Have you tried just using the User HasData() And nesting a single user with it's post, leaving all ID-fields null? Curious if that would work
Patrick
Patrick2y ago
apologies i misread as many:many - you should not be newing up user it'll be setting the user id to 0 and thus be inserting a new user well, in the case of string, won't at all your nav props are set by EF or not at all.
jonathanm
jonathanmOP2y ago
Okay, thank you Is this good?
cs public List<Post> Posts { get; set; } = new List<Post>();
cs public List<Post> Posts { get; set; } = new List<Post>();
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,
});
});
}
Patrick
Patrick2y ago
that's fine
jonathanm
jonathanmOP2y ago
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'.
Patrick
Patrick2y ago
i wouldn't seed data that way 😄 it needs an array, anyway
Nergy101
Nergy1012y ago
Well, that explains itself now doesn't it? =) That's a whole other discussion I think, Focusing on getting his code to work with minimal changes atm hehe
Patrick
Patrick2y ago
u.OwnsOne(s => s.Posts).HasData(new []
{
new Post
{
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
}
});
u.OwnsOne(s => s.Posts).HasData(new []
{
new Post
{
Content = "Hello World!",
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
UserId = newUser.Id,
}
});
Nergy101
Nergy1012y ago
Oh sorry, yeah, that's true!
Patrick
Patrick2y ago
the user won't have an ID either, so you need to use the nav prop. ... ive been here long enough, thanks 😄
jonathanm
jonathanmOP2y ago
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.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server