2013-06-28 11 views
5

Questi sono i miei soggetti:Operazione non riuscita: Il rapporto non potrebbe essere cambiato perché uno o più delle proprietà chiave esterna è non annullabile

public class Currency : Exchange 
{ 
    public List<CurrencyPrice> CurrencyPrice { get; set; } 
} 

public class CurrencyPrice : Price 
{ 
    public int CurrencyId { get; set; } 
    [ForeignKey("CurrencyId")] 
    public Currency Currency { get; set; } 
} 

La prima volta che inserisco un valore nella cosa DB è male, ma se cambio alcun valore nel DB e cerco di inserire o aggiornare il record ottengo questo errore:

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.

questo è il mio codice di inserimento:

var currency = 
    UnitOfWork.Exchange.Get(x => x is Currency && x.ExchangeType.Id == exchangeTypeId) as Currency; 
if (currency != null) 
{ 
    CurrencyPrice lastPriceValue = UnitOfWork.CurrencyPrice.Last(x => x.CurrencyId == currency.Id); 
    if (lastPriceValue == null || lastPriceValue.Value != price) 
    { 
     currency.CurrencyPrice = new List<CurrencyPrice> 
      { 
       new CurrencyPrice {Id = Guid.NewGuid().ToString(), EntryDate = DateTime.Now, Value = price,CurrencyId = currency.Id,Currency = currency} 
      }; 
    } 
    else 
    { 
     lastPriceValue.EntryDate = DateTime.Now; 
    } 

    UnitOfWork.Commit(); 
} 

Ho cercato StackOverflow e ho trovato this. Ma non capisco cosa devo fare.

risposta

10

Credo che il problema sta in questo blocco di codice:

currency.CurrencyPrice = new List<CurrencyPrice> 
{ 
    new CurrencyPrice {Id = Guid.NewGuid().ToString(), EntryDate = DateTime.Now, Value = price,CurrencyId = currency.Id,Currency = currency} 
}; 

Qui si crea una nuova CurrencyPrice e assegnarlo a un Currency, orfano in tal modo l'oggetto CurrencyPrice che è stato assegnato a questo Currency prima. Questo oggetto è ora ancora nel database, ma senza un genitore. Pertanto EntityFramework vuole impostare l'ID del genitore su questo CurrencyPrice su NULL che il database impedisce di provocare l'errore che hai citato. Ho pubblicato una spiegazione più dettagliata di questo problema in this answer.

Per evitare ciò, rimuovere il vecchio CurrencyPrice dal database prima di aggiungerne uno nuovo. In alternativa, poiché sembra che gli oggetti CurrencyPrice dipendano sempre dal loro genitore Currency, si potrebbe considerare di rendere questa una relazione identificativa. Ho spiegato come crearli con EntityFramework 4 Model First al Implementing identifying relationships with EF4. Non ho ancora lavorato con Code First, ma l'approccio dovrebbe essere simile.

+0

Spiegazione molto bella. grazie. –

0

Si è verificato lo stesso errore durante il salvataggio delle modifiche nel contesto e non ha apportato alcuna modifica alle entità o alle relazioni.

Errore perché alcune entità avevano il metodo GetHashCode() ovveridden.

Problemi correlati