public HttpResponseMessage Read()
 {
     using (var dbContext = new PersonContext())
     {
         return this.Request.CreateResponse(HttpStatusCode.OK, dbContext.Persons.ToList());
     }
 }
        public void DannEinHansPeterGeborenAm_SollInDerDatenbankGespeichertSein_(string firstName, string lastName, string birthdate)
        {
            using (var context = new PersonContext())
            {
                var found = context.Persons.Any(x => x.FirstName == firstName && x.LastName == lastName && x.BirthDate == birthdate);

                Assert.IsTrue(found, "Person was not found in database.");
            }
        }
示例#3
0
        private static void CreateDataBase()
        {
            using (var context = new PersonContext())
            {
                if (context.Database.Exists())
                {
                    context.Database.Delete();
                }

                context.Database.Create();
            }
        }
示例#4
0
        public static void List()
        {
            Services.Providers.Message("Cars:");
            using (var c = new CarContext()) {
                foreach (var car in c.Cars) Services.Providers.Message(null, "Key: {0}; Model: {1}", car.Key, car.Model);
            }

            Services.Providers.Message("Persons:");
            using (var c = new PersonContext()) {
                foreach (var p in c.Persons) Services.Providers.Message(null, "Key: {0}; Name: {1}; City: {2}", p.Key, p.Name, p.City);
            }
        }
        public HttpResponseMessage Read(int id)
        {
            using (var dbContext = new PersonContext())
            {
                var result = dbContext.Persons.FirstOrDefault(p => p.Id == id);

                if (result == null)
                {
                    this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "requested person not found!");
                }

                return this.Request.CreateResponse(HttpStatusCode.OK, result);
            }
        }
        public HttpResponseMessage Create(Person person)
        {
            if (person == null)
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.NoContent, "Invalid object!");
            }

            using (var dbContext = new PersonContext())
            {
                dbContext.Persons.Add(person);
                dbContext.SaveChanges();
            }

            return this.Request.CreateResponse(HttpStatusCode.OK, person);
        }
示例#7
0
        public static void Fill()
        {
            using (var p = new PersonContext()) {
                p.Persons.Add(new Person { City="Basel", Name="David" });
                p.Persons.Add(new Person { City="Basel", Name="Bettina" });
                p.Persons.Add(new Person { City="Basel", Name="Sergio" });
                p.Persons.Add(new Person { City="Thun", Name="Kim" });
                p.SaveChanges();
            }

            Services.Providers.Message("Persons Filled.");

            using (var c = new CarContext()) {
                c.Cars.Add(new Car { Model = "Audi" });
                c.Cars.Add(new Car { Model = "BMW" });
                c.Cars.Add(new Car { Model = "Mercedes" });
                c.Cars.Add(new Car { Model = "Fiat" });
                c.SaveChanges();
            }

            Services.Providers.Message("Cars filled.");
        }
示例#8
0
 public HomeController(PersonContext db)
 {
     _db = db;
 }
        public HttpResponseMessage Update(Person person)
        {
            if (person == null)
            {
                return this.Request.CreateErrorResponse(HttpStatusCode.NoContent, "requested person is invalid!");
            }

            using (var dbContext = new PersonContext())
            {
                var personToUpdate = dbContext.Persons.SingleOrDefault(p => p.Id == person.Id);
                if (personToUpdate == null)
                {
                    return this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "requested person not found!");
                }

                Mapper.CreateMap<Person, Person>();
                Mapper.Map(person, personToUpdate);
                dbContext.SaveChanges();

                return this.Request.CreateResponse(HttpStatusCode.OK, personToUpdate);
            }

            
        }
示例#10
0
 public MyRepository(PersonContext context)
 {
     _context = context;
 }
示例#11
0
 public PersonController(ILogger <PersonController> logger, PersonContext context, RutValidator rutValidator)
 {
     _logger       = logger;
     _context      = context;
     _rutValidator = rutValidator;
 }
        public HttpResponseMessage Delete(int id)
        {
            using (var dbContext = new PersonContext())
            {
                var person = dbContext.Persons.FirstOrDefault(p => p.Id == id);

                if (person == null)
                {
                   return  this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "requested person not found!");
                }
                dbContext.Persons.Remove(person);
                dbContext.SaveChanges();
                
                return this.Request.CreateResponse(HttpStatusCode.OK);
            }
        }
 public HttpResponseMessage Seed()
 {
     using (var dbContext = new PersonContext())
     {
         dbContext.Seed();
         dbContext.SaveChanges();
         return this.Request.CreateResponse(HttpStatusCode.OK, dbContext.Persons.ToList());
     }
 }
示例#14
0
 public HomeController(PersonContext db)
 {
     this.db = db;
 }
 public PersonaController(PersonContext context)
 {
     _context = context;
 }
        public static void Run()
        {
            // Ensure database is created and has a person in it
            using (var context = new PersonContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                context.People.Add(new Person { FirstName = "John", LastName = "Doe" });
                context.SaveChanges();
            }

            using (var context = new PersonContext())
            {
                // Fetch a person from database and change phone number
                var person = context.People.Single(p => p.PersonId == 1);
                person.PhoneNumber = "555-555-5555";

                // Change the persons name in the database (will cause a concurrency conflict)
                context.Database.ExecuteSqlCommand("UPDATE dbo.People SET FirstName = 'Jane' WHERE PersonId = 1");

                try
                {
                    // Attempt to save changes to the database
                    context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    foreach (var entry in ex.Entries)
                    {
                        if (entry.Entity is Person)
                        {
                            // Using a NoTracking query means we get the entity but it is not tracked by the context
                            // and will not be merged with existing entities in the context.
                            var databaseEntity = context.People.AsNoTracking().Single(p => p.PersonId == ((Person)entry.Entity).PersonId);
                            var databaseEntry = context.Entry(databaseEntity);

                            foreach (var property in entry.Metadata.GetProperties())
                            {
                                var proposedValue = entry.Property(property.Name).CurrentValue;
                                var originalValue = entry.Property(property.Name).OriginalValue;
                                var databaseValue = databaseEntry.Property(property.Name).CurrentValue;

                                // TODO: Logic to decide which value should be written to database
                                // entry.Property(property.Name).CurrentValue = <value to be saved>;

                                // Update original values to 
                                entry.Property(property.Name).OriginalValue = databaseEntry.Property(property.Name).CurrentValue;
                            }
                        }
                        else
                        {
                            throw new NotSupportedException("Don't know how to handle concurrency conflicts for " + entry.Metadata.Name);
                        }
                    }

                    // Retry the save operation
                    context.SaveChanges();
                }
            }
        }
示例#17
0
 public HomeController(PersonContext db)
 {
     this.db = db;
 }
示例#18
0
 public PersonRepository(PersonContext context) : base(context)
 {
     _context = context;
 }
 public void Setup()
 {
     file = Path.GetTempFileName();
      context = new PersonContext(file);
 }
示例#20
0
 public AddressesController(PersonContext context)
 {
     _context = context;
 }