EntityFramework Code First FluentAPI TPC

时间:2014-02-15 18:07:52

标签: c# entity-framework code-first fluent-interface table-per-class

这里有我的域名实体

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; }
}

映射

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);
    }
}

上下文

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);
    }
}

当我在Nuget Package Console上运行'update-database'命令时,出现错误:

元素中的无效多样性角色“City_Province_Source”连接“City_Province”。由于Dependent Role引用了键属性,因此多重属性Dependent Role的上限必须等于“1”。

1 个答案:

答案 0 :(得分:4)

逻辑上,您正在尝试定义1对多关系。由于City不能包含多个Provinces,因此一个Province可以有多个Cities

在这种情况下,您不一定需要在映射中指定HasRequiredWithMany

CityMap

中删除以下代码
this.HasRequired(x => x.Province)
        .WithMany(x => x.Cities)
        .HasForeignKey(x => x.Id);

ICollection<City>表中有ProvinceProvince表中的属性类型City足以建立关系。

输出将是这样的。

enter image description here