0
我處於MVC4應用程序中,並且正在使用EF CodeFirst。 當我嘗試運行下面的代碼:當我嘗試更新集合屬性時出現UpdateException
public void Autorizare(int cerereId, Persoana persoana)
{
var cerere = _db.Cereri.Find(cerereId);
cerere.Autorizare.Add(persoana);
_db.SaveChanges();
}
我得到這樣的錯誤:
Entities in 'CerereDbContext.Persoane' participate in the 'Actiune_Executanti' relationship. 0 related 'Actiune_Executanti_Source' were found. 1 'Actiune_Executanti_Source' is expected.
我曾嘗試實體(Actiune).STATE = EntityState.Modified,但沒有結果。
我有一個主POCO:
public class Cerere
{
public int Id { get; set; }
...
public virtual ICollection<Actiune> Actiuni { get; set; }
...
}
的Actiune類看起來像這樣
public class Actiune
{
public int Id { get; set; }
public DateTime Data { get; set; }
public String Nume { get; set; }
public virtual ICollection<Persoana> Executanti { get; set; }
public String Stadiu { get; set; }
public String Obs { get; set; }
}
而且單人牀:
public class Persoana
{
public int Id { get; set; }
public DateTime Data { get; set; }
public String Nume { get; set; }
}
in our Cerere:public virtual ICollection Autorizare {get;組; } –
您是否嘗試過設置cerere entitystate進行修改? EF需要知道你已經修改了cerere對象,或者它會嘗試插入persona對象而不更新cerere對象。 –