Here there are my domain entities:
public class Province
{
private ICollection<City> _cities;
public virtual ICollection<City> Cities
{
get { return _cities ?? (_cities = new HashSet<City>()); }
set { _cities = value; }
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual double Latitude { get; set; }
public virtual double Longitude { get; set; }
}
public class City
{
private Province _province;
public virtual Province Province
{
get { return _province ?? (_province = new Province()); }
set { _province = value; }
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Latitude { get; set; }
public virtual string Longitude { get; set; }
}
Mappings:
public class ProvinceMap : EntityTypeConfiguration<Province>
{
public ProvinceMap()
{
this.ToTable("Province");
this.HasKey(p => p.Id);
this.Property(x => x.Id).HasColumnName("Id");
this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(x => x.Name).HasMaxLength(50).IsRequired();
this.Property(x => x.Latitude).IsRequired();
this.Property(x => x.Longitude).IsRequired();
//this.HasMany(x => x.Cities)
// .WithRequired(x => x.Province)
// .HasForeignKey(x => x.Id);
}
}
public class CityMap : EntityTypeConfiguration<City>
{
public CityMap()
{
this.ToTable("City");
this.HasKey(x => x.Id);
this.Property(x => x.Id).HasColumnName("Id");
this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(x => x.Name).HasMaxLength(50).IsRequired();
this.Property(x => x.Latitude).IsRequired();
this.Property(x => x.Longitude).IsRequired();
this.HasRequired(x => x.Province)
.WithMany(x => x.Cities)
.HasForeignKey(x => x.Id);
}
}
Context:
public class DataContext : DbContext
{
public DataContext(): base("DataContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>("DataContext"));
}
public DbSet<Province> Provinces { get; set; }
public DbSet<City> Cities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProvinceMap());
modelBuilder.Configurations.Add(new CityMap());
//base.OnModelCreating(modelBuilder);
}
}
When I run the 'update-database' command at the Nuget Package Console, I have an error:
Invalid multiplicity in the element Role "City_Province_Source" in connection "City_Province". Because the Dependent Role refers to the key properties, the upper bound of the multiplicity properties Dependent Role must be equal to "1".
Logically, you are trying to define a 1-to-many relationship. Because City
cannot be in many Provinces
, and one Province
can have many Cities
.
In this case, you don't necessarily need to specify HasRequired
and WithMany
in your mapping.
Remove the following code from CityMap
this.HasRequired(x => x.Province)
.WithMany(x => x.Cities)
.HasForeignKey(x => x.Id);
Having ICollection<City>
in Province
table, and a property type of Province
in City
table is enough to establish the relationship.
The output will be like this.