I am creating a basic application using ef core. In it I have a Person and Address class. Address is a complex type. When I try to run migration command (add-migration initialmigration) by using Package Manager concole. It throws an exception. It says that I must define Id for this complex type. Below is the following code.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public ICollection<Address> Addresses { get; set; }
}
public class Address
{
public String Street { get; private set; }
private Address()
{
}
public Address(string street)
{
Street = street;
}
}
public class MyContext : DbContext
{
public MyContext() {
}
DbSet<Person> Persons { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=.;Database=MyTest;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().Property(k => k.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Person>(entity => entity.OwnsMany(m => m.Addresses, address =>
address.Property(x => x.Street).HasColumnName("Street")
));
}
}
How could I solve this issues, so that I could migrate my complex type?