2015-11-01 112 views
0

我试图创建一个到多个关系,其中TypeOfImmobile可以有一个Immobile或更多。 这里是我的模型类:Insert语句与外键约束冲突。实体框架错误

INSERT语句冲突与外键约束“FK_dbo.Immobiles_dbo.TypeOfImmobiles_TypeOfImmobilesId”:当我创建一个不动

public class TypeOfImmobile 
{ 
    public int Id { get; set; } 
    [Display(Name = "Type Of Immobile")] 
    public string Designation { get; set; } 

    public ICollection<Immobile> Immobiles { get; set; } 
} 

public class Immobile 
{ 
    public int Id { get; set; } 
    public int TypeOfImmobileId { get; set; } 
    public virtual TypeOfImmobile TypeOfImmobile { get; set; } 
} 

时发生错误。冲突发生在数据库“aspnet-ARQSI-IT2-2015103105205​​6”,表“dbo.TypeOfImmobiles”,列'Id'中。 该声明已被终止。

+0

确切的错误是什么? –

+0

'公共虚拟ICollection Immobiles {get;组; }'也许你错过了'虚拟' –

+0

我试过虚拟它不起作用:/ – sergiogomesdev

回答

0

看起来,当您创建Immobile时,您可能未设置TypeOfImmobileId属性的有效值,即现有的TypeOfImmobileId

0

这导致由以下原因之一:

1-你不设置导航属性(TypeOfImmobile)或TypeOfImmobileId

2-- TypeOfImmobileId或TypeOfImmobile的值在数据库是不存在的

3 - 也许您要保存不动节约TypeOfImmobile

0

之前,您需要通过[ForeignKey("Your_FK_ImmobileId")]在新物业编号设置FK Your_FK_ImmobileId

相关问题