Entity Framework 6 - 导航属性 PK 和 FK 是不同的类型
Entity Framework 6 - Navigation Properties PK and FK are different types
我有一个现有的数据库,它的主键是数字 (18,0),外键是整数。当我尝试使用导航时 属性 EF 抛出一个无效的转换异常。
有没有一种方法可以映射此关系以解决无效转换?
在下面的代码中,promo_cfg.pc_id 是 numeric(18,0),promo.pc_id 是 int。
public class PromotionMap : EntityTypeConfiguration<Promotion>
{
public PromotionMap()
{
// Primary Key
this.HasKey(p => p.PromotionId);
// Properties
// table and column mappings
this.ToTable("promo");
this.Property(p => p.PromotionId).HasColumnName("p_id");
this.Property(p => p.PromotionConfigId).HasColumnName("pc_id");
this.Property(p => p.PromotionCode).HasColumnName("p_code");
this.HasRequired(t => t.PromotionConfig)
.WithMany(t => t.Promotions)
.HasForeignKey(d => new { d.PromotionConfigId });
}
}
public class Promotion
{
public decimal PromotionId { get; set; }
public int PromotionConfigId { get; set; }
public string PromotionCode { get; set; }
}
public PromotionConfigMap()
{
// Primary Key
this.HasKey(s => s.PromotionConfigId);
// Properties
// Table and Column mappings
this.ToTable("promo_cfg");
this.Property(p => p.PromotionConfigId).HasColumnName("pc_id");
this.Property(p => p.AllowOrders).HasColumnName("allow_orders");
this.HasOptional(p => p.Promotions).WithRequired().Map(x => x.MapKey("pc_id"));
}
public class PromotionConfig
{
public int PromotionConfigId { get; set; }
public int AllowOrders { get; set; }
public virtual ICollection<Promotion> Promotions { get; set; }
}
可能是,有一个大的 MAY,有一个非物化的 FK:
HasOptional(x => x.SomeProperty).WithMany().Map(x => x.MapKey("intDbColumnName"));
============================================= ===========
我删除了所有不需要的代码,应该执行以下操作:
public class Promotion
{
public decimal PromotionId { get; set; }
//Here you do not materialize the FK
public PromotionConfig PromotionConfig { get; set; }
}
public class PromotionConfig
{
public decimal PromotionConfigId { get; set; } // you can't do otherwise as
//you must declare a PK for EF
public virtual ICollection<Promotion> Promotions { get; set; }
}
public class PromotionMap : EntityTypeConfiguration<Promotion>
{
public PromotionMap()
{
// Primary Key
this.HasKey(p => p.PromotionId);
// Properties
// table and column mappings
this.ToTable("promo");
this.Property(p => p.PromotionId).HasColumnName("p_id");
//if I well understand pc_id is an int that relates to a decimal
this.HasRequired(p => p.PromotionConfig)
.WithMany(pc => pc.Promotions)
.Map(p => p.MapKey("pc_id"));
}
}
public class PromotionConfigMap : EntityTypeConfiguration<PromotionConfig> {
public PromotionConfigMap()
{
// Primary Key
this.HasKey(s => s.PromotionConfigId);
// Properties
// Table and Column mappings
this.ToTable("promo_cfg");
}
}
我有一个现有的数据库,它的主键是数字 (18,0),外键是整数。当我尝试使用导航时 属性 EF 抛出一个无效的转换异常。
有没有一种方法可以映射此关系以解决无效转换?
在下面的代码中,promo_cfg.pc_id 是 numeric(18,0),promo.pc_id 是 int。
public class PromotionMap : EntityTypeConfiguration<Promotion>
{
public PromotionMap()
{
// Primary Key
this.HasKey(p => p.PromotionId);
// Properties
// table and column mappings
this.ToTable("promo");
this.Property(p => p.PromotionId).HasColumnName("p_id");
this.Property(p => p.PromotionConfigId).HasColumnName("pc_id");
this.Property(p => p.PromotionCode).HasColumnName("p_code");
this.HasRequired(t => t.PromotionConfig)
.WithMany(t => t.Promotions)
.HasForeignKey(d => new { d.PromotionConfigId });
}
}
public class Promotion
{
public decimal PromotionId { get; set; }
public int PromotionConfigId { get; set; }
public string PromotionCode { get; set; }
}
public PromotionConfigMap()
{
// Primary Key
this.HasKey(s => s.PromotionConfigId);
// Properties
// Table and Column mappings
this.ToTable("promo_cfg");
this.Property(p => p.PromotionConfigId).HasColumnName("pc_id");
this.Property(p => p.AllowOrders).HasColumnName("allow_orders");
this.HasOptional(p => p.Promotions).WithRequired().Map(x => x.MapKey("pc_id"));
}
public class PromotionConfig
{
public int PromotionConfigId { get; set; }
public int AllowOrders { get; set; }
public virtual ICollection<Promotion> Promotions { get; set; }
}
可能是,有一个大的 MAY,有一个非物化的 FK:
HasOptional(x => x.SomeProperty).WithMany().Map(x => x.MapKey("intDbColumnName"));
============================================= ===========
我删除了所有不需要的代码,应该执行以下操作:
public class Promotion
{
public decimal PromotionId { get; set; }
//Here you do not materialize the FK
public PromotionConfig PromotionConfig { get; set; }
}
public class PromotionConfig
{
public decimal PromotionConfigId { get; set; } // you can't do otherwise as
//you must declare a PK for EF
public virtual ICollection<Promotion> Promotions { get; set; }
}
public class PromotionMap : EntityTypeConfiguration<Promotion>
{
public PromotionMap()
{
// Primary Key
this.HasKey(p => p.PromotionId);
// Properties
// table and column mappings
this.ToTable("promo");
this.Property(p => p.PromotionId).HasColumnName("p_id");
//if I well understand pc_id is an int that relates to a decimal
this.HasRequired(p => p.PromotionConfig)
.WithMany(pc => pc.Promotions)
.Map(p => p.MapKey("pc_id"));
}
}
public class PromotionConfigMap : EntityTypeConfiguration<PromotionConfig> {
public PromotionConfigMap()
{
// Primary Key
this.HasKey(s => s.PromotionConfigId);
// Properties
// Table and Column mappings
this.ToTable("promo_cfg");
}
}