简体   繁体   中英

Add related object with Entity Framework

I have a problem that I can't solve ... using Entity Framework, i have 2 objects :

Opérateurs :

public partial class Opérateurs
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Opérateurs()
    {
        Domaines = new HashSet<Domaines>();
        Contacts = new HashSet<Contacts>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int IdOpérateurs { get; set; }

    [StringLength(30)]
    public string Adresse { get; set; }

    [StringLength(10)]
    public string CodePostal { get; set; }

    [StringLength(25)]
    public string Ville { get; set; }

    [Required]
    [StringLength(30)]
    public string Société { get; set; }

    [StringLength(60)]
    public string Email { get; set; }

    [StringLength(20)]
    public string Fixe { get; set; }

    [StringLength(20)]
    public string Mobile { get; set; }

    public Boolean isActifDansCaveAVin { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Domaines> Domaines { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Contacts> Contacts { get; set; }
}

}

and Domaines :

public partial class Domaines
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Domaines()
    {
        Vins = new HashSet<Vins>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int idDomaine { get; set; }

    [Required]
    [StringLength(200)]
    public string NomDomaine { get; set; }

    public int idOpérateur { get; set; }

    public virtual Opérateurs Opérateurs { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Vins> Vins { get; set; }
}

I'm able to add Opérateur (great !) but I can't add Domaines :-(

Here is my Unit Test :

[TestMethod]
    public void AjouterDomaine()
    {
        using (CaveAVinContext context = new CaveAVinContext())
        {
            Opérateurs operateur = context.Opérateurs.Find(500111);

            Domaines domaine = new Domaines();
            domaine.idDomaine = 5;
            domaine.Opérateurs = operateur;
            domaine.NomDomaine = "domaine Eric test";

            context.Domaines.Add(domaine);
            SaveChanges(context);
        }
    }

Doing this, I get an DbUpdateException (with no indication :-( ) NB : I also tried with this line in comment (domaine.idDomaine = 5;) => same result

What's wrong ?

I think there are two things wrong:

You are setting the Id field on an auto-generated field, and you maybe added in the domain twice, on lines:

domaine.Opérateurs = operateur;

and

context.Domaines.Add(domaine);

Try:

Remove: domaine.Opérateurs = operateur;

Add: domaine.OpérateursId = operateur.OpérateursId;

Keep context.Domaines.Add(domaine);

If that works, I will add more explanation in this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM