0

I want to seed data with Bogus, but I get one error again and again trying to add migration.

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options): base(options){ }

    public DbSet<User> Users { get; set; } = null!;
    public DbSet<Recipe> Recipes { get; set; } = null!;
    

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Recipe>()
            .HasOne(r => r.Author)
            .WithMany(u => u.OwnedRecipes)
            .HasForeignKey(r => r.AuthorId);
        
        var fakeUsers= new Faker<User>()
            .RuleFor(o => o.Id, _ => Guid.NewGuid())
            .RuleFor(o => o.Name, f => f.Person.FullName)
            .RuleFor(o => o.Email, (f, o) => f.Internet.Email(o.Name))
            .RuleFor(o => o.Password, f => f.Internet.Password())
            .RuleFor(o => o.ProfileImageUrl, f => f.Internet.Avatar())
            .RuleFor(o => o.UpdatedAt, f => f.Date.Past())
            .RuleFor(o => o.CreatedAt, (f, o) => f.Date.Past(refDate: o.UpdatedAt));
        var dataUsers = fakeUsers.Generate(100);
        var fakeRecipes = new Faker<Recipe>()
            .RuleFor(o => o.Id, _ => Guid.NewGuid())
            .RuleFor(o => o.Title, f => f.Commerce.ProductName())
            .RuleFor(o => o.ImageUrl, (f, o) => f.Image.LoremPixelUrl("Food"))
            .RuleFor(o => o.Description, f => f.Commerce.ProductDescription())
            .RuleFor(o => o.Likes, f => f.Random.Int(0, 2000))
            .RuleFor(o => o.Comments, f => f.Random.Int(0, 100))
            .RuleFor(o => o.UpdatedAt, (f,o) => f.Date.Past())
            .RuleFor(o => o.CreatedAt, (f, o) => f.Date.Past(refDate: o.UpdatedAt))
            .RuleFor(o => o.Author, _ => dataUsers[new Random().Next(dataUsers.Count)])
            .RuleFor(o => o.AuthorId, (f,o) => o.Author.Id);
        var dataRecipes = fakeRecipes.Generate(600);
        
        
        foreach (var user in dataUsers)
        {
            user.OwnedRecipes = dataRecipes.Where(r => user.Id == r.AuthorId).ToList();
        }
        
        
        
        modelBuilder.Entity<User>().HasData(dataUsers);
        modelBuilder.Entity<Recipe>().HasData(dataRecipes);
        
    }
}
public class Recipe : BaseEntity
{
    [ForeignKey("AuthorId")] 
    public virtual User Author { get; set; } 
    public Guid AuthorId { get; set; }

    public string ImageUrl { get; set; }
    
    public string Title { get; set; }
    public string? Description { get; set; }
    
    public int Likes { get; set; }
    public int Comments { get; set; }
}
public class User : BaseEntity
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string ProfileImageUrl { get; set; }

    public virtual ICollection<Recipe> OwnedRecipes { get; set; }
}
public class BaseEntity
{
    public BaseEntity()
    {
        CreatedAt = UpdatedAt = DateTime.UtcNow;
    }

    [Key]
    public Guid Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

I get this error :

The seed entity for entity type 'Recipe' with the key value 'Id:68ef72c4-e63e-419a-94e5-6b0ee5f5d880' cannot be added because it has the navigation 'Author' set. To seed relationships, add the entity seed to 'Recipe' and specify the foreign key values {'AuthorId'}.

I tried to use .OwnsMany or .OwnsOne, but then I get

The entity type 'User' cannot be configured as owned because it has already been configured as a non-owned. If you want to override previous configuration first remove the entity type from the model by calling 'Ignore'.

Setup
  • 1
  • 1
  • If setting the `AuthorId` the virtual property must not be set, try rewriting the recipie rule to not set the `o.Author` and only set `o.AuthorId` instead. – Raldo94 Apr 29 '23 at 18:36

0 Answers0