I am trying to create a Condominium Data Model in ASP.NET with SQL Server. I stumbled upon the creation of the proprietario(1)
relation with fracao(many)
.
The error returned is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Fracao_dbo.Proprietario_ProprietarioID". The conflict occurred in database "WebCond", table "dbo.Proprietario", column 'ProprietarioID'.
My classes:
[Table("Fracao")]
public class Fracao
{
[ScaffoldColumn(false)]
public int FracaoID { get; set; }
public int? ProprietarioID { get; set; }
public virtual Proprietario Proprietario { get; set; }
[Required]
public int CondominioID { get; set; }
public virtual Condominio Condominio { get; set; }
[Required]
public int ZonaID { get; set; }
public virtual Zona Zona { get; set; }
[Required, StringLength(4), Display(Name = "Letra")]
public string Letra { get; set; }
[Required, Display(Name = "Área")]
public decimal Area { get; set; }
[Required, Display(Name = "Permilagem")]
public decimal Permilagem { get; set; }
[Required, StringLength(4), Display(Name = "Piso")]
public string Piso { get; set; }
[Required, StringLength(10), Display(Name = "Porta")]
public string Porta { get; set; }
}
[Table("Proprietario")]
public class Proprietario
{
[ScaffoldColumn(false)]
public int ProprietarioID { get; set; }
[Required, StringLength(255), Display(Name = "Nome")]
public string Nome { get; set; }
[Required, StringLength(500), Display(Name = "Morada"), DataType(DataType.MultilineText)]
public string Morada { get; set; }
[Required, StringLength(30), Display(Name = "CPostal")]
public string CPostal { get; set; }
[Required, StringLength(100), Display(Name = "Localidade")]
public string Localidade { get; set; }
[StringLength(10), Display(Name = "Telefone")]
public string Telefone { get; set; }
[StringLength(10), Display(Name = "Telemovel")]
public string Telemovel { get; set; }
[DataType(DataType.EmailAddress), Display(Name = "Email")]
public string Email { get; set; }
[StringLength(10), Display(Name = "Contribuinte")]
public string Contribuinte { get; set; }
public virtual ICollection<Fracao> Fracoes { get; set; }
}
I tried to create the entities in the way that I created others and they are fine, but these are not.
It seems to me that the problem is related to cascade delete , and I have to create the appropriate exceptions, but I do not know which ones.