Entity framework 实体框架6-导航属性PK和FK是不同的类型
我有一个现有数据库,它的PK是numberic18,0,FK是int。当我尝试使用导航属性时,EF抛出一个无效的强制转换异常 有没有一种方法可以映射这种关系,从而对无效的强制转换进行排序 在下面的代码中,promo_cfg.pc_id为数字18,0,promo.pc_id为intEntity framework 实体框架6-导航属性PK和FK是不同的类型,entity-framework,mapping,Entity Framework,Mapping,我有一个现有数据库,它的PK是numberic18,0,FK是int。当我尝试使用导航属性时,EF抛出一个无效的强制转换异常 有没有一种方法可以映射这种关系,从而对无效的强制转换进行排序 在下面的代码中,promo_cfg.pc_id为数字18,0,promo.pc_id为int public class PromotionMap : EntityTypeConfiguration<Promotion> { public PromotionMap() {
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; }
}
可能是,用一个大的可能,用一个非物化的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");
}
}
谢谢,但这不起作用。我仍然得到以下异常base={从物化的'System.Decimal'类型到'System.Int32'类型的指定强制转换无效。}应该还有其他内容。您提供的类型是CLR类型。但在给定的配置中,FK并没有具体化,所以不应该出现具体化。请您再给我们一些您的代码,例如隐式类,好吗?请原谅我说得太慢,但我没有按照您在回答中所说的去做。如果HasRequired是正确的,那么我所需要的一切在我的配置中哪里出错了?正如您所看到的,我创建了PromotionConfig.PromotionConfigId作为int,但在数据库中它是一个数字18,0。如果我在我的类中将其更改为十进制,那么ef会抱怨无法映射不同类型的属性。虽然这并不能完全解决我的问题,但您的持久性最终让我达到了目的。PromotionConfigMap需要指定的类型。this.Propertyp=>p.PromotionCofigId.HasColumnNamepc\u id.HasColumnTypedecimal