示例#1
0
        public CarWithPartsModel CarWithParts(int id)
        {
            if (!this.db.Cars.Any(c => c.Id == id))
            {
                return(default(CarWithPartsModel));
            }

            var car = this.db.Cars.First(c => c.Id == id);

            db.Entry(car).Collection(c => c.Parts).Load();
            car.Parts.ToList().ForEach(p => this.db.Entry(p).Reference(pp => pp.Part).Load());

            var result = new CarWithPartsModel()
            {
                Make              = car.Make,
                Model             = car.Model,
                TravelledDistance = car.TravelledDistance,
                Parts             = car.Parts.Select(p => new PartModel
                {
                    Name  = p.Part.Name,
                    Price = p.Part.Price
                }).ToList()
            };

            return(result);
        }
示例#2
0
 public virtual void Update(T entity)
 {
     dbSet.Attach(entity);
     dataContext.Entry(entity).State = EntityState.Modified;
 }