I try to add a migration for the below entities and i get the error. I've tried everything to get it to work and i feel like i'm missing something obvious. The code is below
"The seed entity for type 'Bug' cannot be added because no value was provided for the required property 'UserId'"
public class Bug
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BugId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public Status Status { get; set; }
public DateTime DateCreated { get; set; }
public User User { get; set; }
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
modelBuilder.Entity<Bug>().HasData(
new Bug()
{
BugId = 1,
Title = "TestTitle1",
Description = "Test1Description1",
DateCreated = DateTime.Now,
Status = Status.Closed,
User = new User() { UserId = 1, UserName = "TestUser1" },
}
I have tried the above as well as looking up shadow keys and seperating Bug and User entitities
CodePudding user response:
Firstly, you need to add UserId
property in your Bug
class and mark it as a foreign key. Then have to seed the User
record and then refer it in your Bug
record.
Change your classes as below:
public class Bug
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BugId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public Status Status { get; set; }
public DateTime DateCreated { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public ICollection<Bug> Bugs { get; set; }
}
And then make this change for modelBuilder:
modelBuilder.Entity<Bug>().HasData(
new User()
{
UserId = 1,
UserName = "TestUser1"
}
);
modelBuilder.Entity<Bug>().HasData(
new Bug()
{
BugId = 1,
Title = "TestTitle1",
Description = "Test1Description1",
DateCreated = DateTime.Now,
Status = Status.Closed,
UserId = 1,
}
);
Please check more details here
CodePudding user response:
UPDATE:
Thanks for all your suggestions. I spent a few hours today messing about with it and below is how i managed to get it to work in case anyone else comes across this error.
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UserName { get; set; }
public ICollection<Bug> Bugs{ get; set; }
}
public class Bug
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public Status Status { get; set; }
public DateTime DateCreated { get; set; }
public User User { get; set; }
public int UserId { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var user1 = new User()
{
Id = 1,
UserName = "TestUsername1"
};
modelBuilder.Entity<User>().HasData(user1);
modelBuilder.Entity<Bug>().HasData(
new Bug()
{
Id = 1,
Title = "TestTitle1",
Description = "TestDescription1",
DateCreated = DateTime.Now,
Status = Status.Open,
UserId = 1,
});
}