Usted puede encontrar el código fuente que demuestra esta cuestión @http://code.google.com/p/contactsctp5/CTP5 EF Código primera pregunta
tengo tres objetos del modelo. Contacto, ContactInfo, ContactInfoType. Donde un contacto tiene muchas contactinfo y cada contactinfo es un tipo de contacto. Bastante simple, supongo. El problema que me estoy encontrando es cuando voy a editar el objeto de contacto. Lo saqué de mi repositorio de contactos. Luego ejecuto "UpdateModel (contacto);" y actualiza el objeto con todos los valores de mi formulario. (Monitoreo con depuración) Cuando guardo los cambios, sin embargo, me sale el siguiente error:
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.
Parece que después de que yo llamo modelo de actualización que anula mis referencias y esto parece romper todo? Cualquier idea sobre cómo remediar sería muy apreciada. Gracias.
Éstos son mis modelos:
acciónpublic 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; }
}
Mi controlador:
[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);
}
}
Código Contexto:
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);
}
¿Es el error independiente del tipo de cambios que realiza en su formulario? Me imagino que tal error puede ocurrir si eliminas una 'ContactInformation' de la colección en' Contact'. ¿El error también ocurre si solo cambias, por ejemplo, el 'FirstName' en tu formulario y nada más? – Slauma