我有一個機場實體參考另一個實體,每個實體都有自己的引用。使用EntityState.Modified更新EF 6 DbContex失敗
嘗試更新的機場,我有一些例外.. 繼承人的代碼試圖更新機場
var airport = context.Airports
.Where(a => a.AirportID == airportToUpdate.AirportID).FirstOrDefault();
var ai = AirportConverter.ToModel(airportToUpdate);
ai.AirportID = airport.AirportID;
airport.IsOpenForFlights = ai.IsOpenForFlights;
airport.EmergencyLandings = new List<Scenario>();
foreach (var emergency in ai.EmergencyLandings)
{
var sce = context.Scenarios
.Include(s => s.Mission).Include(s => s.Planes)
.Where(s => s.ScenarioID == emergency.ScenarioID).First();
airport.EmergencyLandings.Add(sce);
context.Entry(sce).State = EntityState.Modified;
}
airport.ExistingFilghtNumbers = new List<string>();
foreach (var flight in ai.ExistingFilghtNumbers)
{
airport.ExistingFilghtNumbers.Add(flight);
context.Entry(flight).State = EntityState.Modified;
}
var parkingLot = ai.ParkingLots.First();
var pLot = context.ParkingLots
.Include(p => p.AtAirport)
.Include(p => p.ParkingStations.Select(pa => pa.PlaneParking))
.Where(p => p.ParkingLotID == parkingLot.ParkingLotID).First();
airport.ParkingLots = new List<ParkingLot>();
airport.ParkingLots.Add(pLot);
context.Entry(pLot).State = EntityState.Modified;
airport.Queues = new List<PlanesQueue>();
foreach (var queue in ai.Queues)
{
qu = context.PlanesQueues
.Include(p => p.Planes.Select(pl => pl.AtQueue))
.Where(q => q.QueueNumber == queue.QueueNumber).First();
qu.Planes = queue.Planes;
airport.Queues.Add(qu);
context.Entry(qu).State = EntityState.Modified;
}
airport.Routes = new List<Route>();
foreach (var route in ai.Routes)
{
var ro = context.Routes
.Include(r => r.PlaneOnRoute)
.Where(r => r.RouteID == route.RouteID).First();
airport.Routes.Add(route);
context.Entry(route).State = EntityState.Modified;
}
//var entry = context.Entry(airport);
//entry.State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
我已經嘗試過的最簡單的更新,直到我有這個代碼.. 試圖先刪除實體,然後再次添加它,並試圖在更新機場本身之前更新每個實體。
,它仍然無法正常工作,就像
當然保存或接受改變許多例外失敗,因爲類型「FlightsControl.Dal.Entities.Route」的不止一個實體具有相同的主鍵值。確保顯式設置的主鍵值是唯一的。確保數據庫生成的主鍵在數據庫和實體框架模型中配置正確。使用實體設計器進行數據庫優先/模型優先配置。使用「HasDatabaseGeneratedOption」一口流利的API或‘DatabaseGeneratedAttribute’的代碼首先配置
將具有相同的主鍵,我試圖更新這並不僅僅是爲了增加它..
這是機場實體
public virtual int AirportID { get; set; }
public ICollection<PlanesQueue> Queues { get; set; }
public ICollection<ParkingLot> ParkingLots { get; set; }
public ICollection<Route> Routes { get; set; }
public virtual ICollection<string> ExistingFilghtNumbers { get; set; }
public virtual bool IsOpenForFlights { get; set; }
public ICollection<Scenario> EmergencyLandings { get; set; }
機場配置
public AirportConfig()
{
this.HasKey(a => a.AirportID);
this.Property(a => a.AirportID)
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
this.HasMany(a => a.Queues).WithRequired(q => q.AtAirport);
this.HasMany(a => a.ParkingLots).WithRequired(p => p.AtAirport);
this.HasMany(a => a.Routes).WithRequired(r => r.AtAirport);
this.HasMany(a => a.EmergencyLandings).WithRequired(s => s.AtAirport);
}
那麼,在Route的特定情況下,您正在查詢並將其返回給'var ro = context.Routes.Include(r => r.PlaneOnRoute)中的變量。其中(r => r.RouteID = = route.RouteID).First();',但正在使用另一個實體添加和標記爲在context.Entry(route).State = EntityState.Modified;'模型中被修改。 –