4
I am trying to create a Condominium data model in ASP.NET with SQL Server. I stumbled on the creation of the interface proprietario(1)
with the 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 the way I created others that are fine, but these.
It seems to me that the problem is related to the Cascade delete, and that I have to create the appropriate exceptions, but I don’t know which ones.
You have no mapping class, all mapping is done in these two classes ?
– BetaSystems - Rodrigo Duarte
I have no mapping class. This is the solution?
– ASoares
The owner you are trying to insert in the Fraction table, exists in the Owner table?
– Jhonatas Kleinkauff
@Betasystems-Rodrigoduarte No
Code First
a scheme calledconvention over configuration
so you don’t need another class for mapping.– Vitor Canova
@Jhonatas Kleinkauff In the BD boot class I have the data I think is necessary. Ex: new Fracao { Fracaoid = 1, Condominioid=1, Zonaid=2, Proprietarioid=1, Letter = "A", Piso="-4", Porta="GAR 19", Area=34.7m, Permilage=4.6m, }
– ASoares
Owner: Proprietarioid= 1, Name= "Proprietary name", Address= "Prop address", Cpostal= "1000-285", Locality= "Lisboa", Telefone= "", Telemovel= "", Email= "", Contributor= "", Contributor= "", ; },
– ASoares
@Vitorcanova, it is not a rule that, can be done the mapping.
– BetaSystems - Rodrigo Duarte
@Asoares, it is not necessary, was another question to understand how its structure is. All the other classes are exactly like this, right ?
– BetaSystems - Rodrigo Duarte
@Yes, I only put it because I thought you were looking for that link file when using the first
Entity
.– Vitor Canova