Vous pouvez trouver le code source démontrant cette question @http://code.google.com/p/contactsctp5/code CTP5 EF Première question
J'ai trois objets de modèle. Contact, ContactInfo, ContactInfoType. Où un contact a beaucoup de contactinfo et chaque contactinfo est uninfotype de contact. Assez simple je suppose. Le problème que je rencontre est quand je vais éditer l'objet contact. Je l'ai tiré de mon dépôt de contact. Ensuite, je cours "UpdateModel (contact);" et il met à jour l'objet avec toutes les valeurs de mon formulaire. (Surveillance avec débogage) Lorsque j'enregistre les changements que, je reçois l'erreur suivante:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Il semble que, après que j'appelle le modèle de mise à jour, il NULLS mes références et cela semble tout casser? Toute idée sur la façon de remédier serait grandement appréciée. Merci.
Voici mes modèles:
public partial class Contact {
public Contact() {
this.ContactInformation = new HashSet<ContactInformation>();
}
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<ContactInformation> ContactInformation { get; set; }
}
public partial class ContactInformation {
public int ContactInformationId { get; set; }
public int ContactId { get; set; }
public int ContactInfoTypeId { get; set; }
public string Information { get; set; }
public virtual Contact Contact { get; set; }
public virtual ContactInfoType ContactInfoType { get; set; }
}
public partial class ContactInfoType {
public ContactInfoType() {
this.ContactInformation = new HashSet<ContactInformation>();
}
public int ContactInfoTypeId { get; set; }
public string Type { get; set; }
public virtual ICollection<ContactInformation> ContactInformation { get; set; }
}
Mon contrôleur d'action:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Contact person) {
if (this.ModelState.IsValid) {
var contact = this.contactRepository.GetById(person.ContactId);
UpdateModel(contact);
this.contactRepository.Save();
TempData["message"] = "Contact Saved.";
return PartialView("Details", contact);
} else {
return PartialView(person);
}
}
Code Contexte:
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {
modelBuilder.Entity<Contact>()
.HasMany(c => c.ContactInformation)
.WithRequired()
.HasForeignKey(c => c.ContactId);
modelBuilder.Entity<ContactInfoType>()
.HasMany(c => c.ContactInformation)
.WithRequired()
.HasForeignKey(c => c.ContactInfoTypeId);
}
L'erreur est-elle indépendante du type de modifications que vous effectuez sur votre formulaire? Je pourrais imaginer qu'une telle erreur peut se produire si vous supprimez un 'ContactInformation' de la collection dans' Contact'. Est-ce que l'erreur se produit également si vous changez seulement par exemple le 'FirstName' sur votre formulaire et rien d'autre? – Slauma