public Address Create(Address address)
        {
            //_ctx.DetachAll();

            //_ctx.Attach(address.City).State = EntityState.Unchanged;
            var addressEntry = _ctx.Add(new AddressSql
            {
                Id         = address.Id,
                Additional = address.Additional,
                CityId     = address.City?.ZipCode ?? 0,
                StreetName = address.StreetName,
                StreetNr   = address.StreetNr
            });

            _ctx.SaveChanges();
            return(new Address()
            {
                Id = addressEntry.Entity.Id,
                Additional = addressEntry.Entity.Additional,
                City = new City()
                {
                    ZipCode = addressEntry.Entity.CityId
                },
                StreetName = addressEntry.Entity.StreetName,
                StreetNr = addressEntry.Entity.StreetNr
            });
        }
Exemplo n.º 2
0
 public City Create(City city)
 {
     try
     {
         var cityEntry = _ctx.Add(_mapper.Map <City, CitySql>(city));
         _ctx.SaveChanges();
         var newCity = cityEntry.Entity;
         return(_mapper.Map <CitySql, City>(newCity));
     }
     catch (Exception e)
     {
         throw new DataSourceException(e.InnerException?.Message);
     }
 }
        public Customer Create(Customer customer)
        {
            //_ctx.Attach(customer.Address).State = EntityState.Unchanged;
            var customerEntity = new CustomerSql
            {
                FirstName = customer.FirstName,
                LastName  = customer.LastName,
                AddressId = customer.Address.Id
            };

            var customerEntry = _ctx.Add(customerEntity);

            _ctx.SaveChanges();
            return(new Customer
            {
                Id = customerEntry.Entity.Id,
                LastName = customerEntry.Entity.LastName,
                FirstName = customerEntry.Entity.FirstName,
                Address = new Address
                {
                    Id = customer.Address.Id
                }
            });
        }