private int CreateEngineIfNotExist(Engine engine, CarsSaleEntities context) { var dbEngine = context.ENGINEs .Include(x => x.ENGINE_FUEL) .FirstOrDefault(x => x.VOLUME == engine.Volume); var existWithFuel = dbEngine?.ENGINE_FUEL.Select(f => f.ID) .SequenceEqual(engine.Fuels.Select(ef => ef.Id)) ?? false; if (existWithFuel) { return(dbEngine.ID); } dbEngine = context.ENGINEs.Add(engine.Convert()); context.ENGINE_FUEL.AddRange( engine.Fuels.Select(x => new ENGINE_FUEL { ENGINE_ID = dbEngine.ID, FUEL_ID = x.Id })); return(dbEngine.ID); }
private int CreateVehiclIfNotExists(Vehicl vehicl, CarsSaleEntities context) { vehicl.Engine.Id = CreateEngineIfNotExist(vehicl.Engine, context); var dbVehicl = context.VEHICLs .FirstOrDefault(v => v.BRAND_ID == vehicl.Brand.Id && v.VEHICL_TYPE_ID == vehicl.VehiclType.Id && v.ENGINE_ID == vehicl.Engine.Id && v.TRANSMISSION_TYPE_ID == vehicl.TransmissionType.Id && v.ENGINE_ID == vehicl.Engine.Id); dbVehicl = dbVehicl ?? context.VEHICLs.Add(vehicl.Convert()); return(dbVehicl.ID); }
private Advertisement Get(int id, CarsSaleEntities context) { var db = context.ADVERTISEMENTs .Where(adv => adv.ID == id) .Include(adv => adv.User) .Include(adv => adv.REGION) .Include(adv => adv.VEHICL) .Include(adv => adv.VEHICL.BRAND) .Include(adv => adv.VEHICL.ENGINE) .Include(adv => adv.VEHICL.TRANSMISSION_TYPE) .Include(adv => adv.VEHICL.VEHICL_TYPE) .Include(adv => adv.VEHICL.ENGINE.ENGINE_FUEL.Select(f => f.FUEL)) .FirstOrDefault(); return(db == null ? null : new Advertisement(db)); }