[英]EntityFramework Code First FluentAPI TPC
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: 在Nuget软件包控制台上运行“ update-database”命令时,出现错误:
Invalid multiplicity in the element Role "City_Province_Source" in connection "City_Province". 连接“ City_Province”中的元素角色“ City_Province_Source”中的无效多重性。 Because the Dependent Role refers to the key properties, the upper bound of the multiplicity properties Dependent Role must be equal to "1". 因为从属角色是指关键属性,所以多重性属性从属角色的上限必须等于“ 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
. 因为City
不能在许多Provinces
,一个Province
可以有许多Cities
。
In this case, you don't necessarily need to specify HasRequired
and WithMany
in your mapping. 在这种情况下,您不必在映射中指定HasRequired
和WithMany
。
Remove the following code from CityMap
从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. 在Province
表中具有ICollection<City>
,在City
表中具有Province
的属性类型足以建立关系。
The output will be like this. 输出将是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.