Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                var destination = new Destination
                {
                    Country     = "Indonesia",
                    Description = "EcoTourism at its best in exquisite Bali",
                    Name        = "Bali"
                };

                var lodging = new Lodging
                {
                    Name = "Rainy Day Motel",
                };

                var cstr = @"Server=DESKTOP-D51E9P0; Database=BreakAway;User ID=sa;Password=l88888888";
                //Data Source=DESKTOP-D51E9P0;Persist Security Info=True;User ID=sa;Password=***********
                //Data Source=DESKTOP-D51E9P0;Initial Catalog=master;Persist Security Info=True;User ID=sa;Password=***********
                using (var connection = new SqlConnection(cstr))
                {
                    using (var context = new BreakAwayContext(connection))
                    {
                        context.Lodgings.Add(lodging);
                        context.SaveChanges();
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                Console.WriteLine(" 保存失败! 错误信息:" + ex.Message);
            }
            Console.WriteLine("OK");
            Console.Read();
        }
        static void RunTest()
        {
            using (var context = new BreakAwayContext())
            {
                context.Database.Initialize(force: true);

                context.Destinations.Add(new Destination {
                    Name = "Fiji"
                });
                context.SaveChanges();
            }
            using (var context = new BreakAwayContext())
            {
                if (context.Destinations.Count() == 1)
                {
                    Console.WriteLine(
                        "Test Passed: 1 destination saved to database");
                }
                else
                {
                    Console.WriteLine(
                        "Test Failed: {0} destinations saved to database",
                        context.Destinations.Count());
                }
            }
        }
Пример #3
0
        private static void DeleteDestinationInMemoryAndDbCascade()
        {
            int destinationId;

            using (var context = new BreakAwayContext())
            {
                var destination = new Destination
                {
                    Name     = "Sample Destination",
                    Lodgings = new List <Lodging>
                    {
                        new Lodging {
                            Name = "Lodging One"
                        },
                        new Lodging {
                            Name = "Lodging Two"
                        }
                    }
                };
                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }
            using (var context = new BreakAwayContext())
            {
                var destination = context.Destinations
                                  .Include("Lodgings")
                                  .Single(d => d.DestinationId == destinationId);
                var aLodging = destination.Lodgings.FirstOrDefault();
                context.Destinations.Remove(destination);
                Console.WriteLine("State of one Lodging: {0}",
                                  context.Entry(aLodging).State.ToString());
                context.SaveChanges();
            }
        }
Пример #4
0
        private static void SaveDestinationAndLodgings(
            Destination destination,
            List <Lodging> deletedLodgings)
        {
            // TODO: Ensure only Destinations & Lodgings are passed in
            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(destination);

                if (destination.DestinationId > 0)
                {
                    context.Entry(destination).State = EntityState.Modified;
                }

                foreach (var lodging in destination.Lodgings)
                {
                    if (lodging.LodgingId > 0)
                    {
                        context.Entry(lodging).State = EntityState.Modified;
                    }
                }

                foreach (var lodging in deletedLodgings)
                {
                    context.Entry(lodging).State = EntityState.Deleted;
                }

                context.SaveChanges();
            }
        }
        private static void AddMultipleDestinations()
        {
            using (var context = new BreakAwayContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;

                context.Destinations.Add(new Destination
                {
                    Name    = "Paris",
                    Country = "France"
                });

                context.Destinations.Add(new Destination
                {
                    Name    = "Grindelwald",
                    Country = "Switzerland"
                });

                context.Destinations.Add(new Destination
                {
                    Name    = "Crete",
                    Country = "Greece"
                });

                context.SaveChanges();
            }
        }
Пример #6
0
        private static void ApplyChanges <TEntity>(TEntity root) where TEntity : class, IObjectWithState
        {
            using (var context = new BreakAwayContext())
            {
                context.Set <TEntity>().Add(root);

                CheckForEntitiesWithoutStateInterface(context);

                foreach (var entry in context.ChangeTracker
                         .Entries <IObjectWithState>())
                {
                    IObjectWithState stateInfo = entry.Entity;
                    if (stateInfo.State == State.Modified)
                    {
                        entry.State = EntityState.Unchanged;
                        foreach (var property in stateInfo.ModifiedProperties)
                        {
                            entry.Property(property).IsModified = true;
                        }
                    }
                    else
                    {
                        entry.State = ConverterState(stateInfo.State);
                    }
                    entry.State = ConverterState(stateInfo.State);
                }

                context.SaveChanges();
            }
        }
        private static void ReuseDbConnection()
        {
            var cstr = @"Server=.\SQLEXPRESS;
        Database=BreakAwayDbConnectionConstructor;
        Trusted_Connection=true";

            using (var connection = new SqlConnection(cstr))
            {
                using (var context = new BreakAwayContext(connection))
                {
                    context.Destinations.Add(new Destination {
                        Name = "Hawaii"
                    });
                    context.SaveChanges();
                }

                using (var context = new BreakAwayContext(connection))
                {
                    foreach (var destination in context.Destinations)
                    {
                        Console.WriteLine(destination.Name);
                    }
                }
            }
        }
        private static void AddSimpleGraph()
        {
            var essex = new Destination
            {
                Name     = "Essex, Vermont",
                Lodgings = new List <Lodging>
                {
                    new Lodging {
                        Name = "Big Essex Hotel"
                    },
                    new Lodging {
                        Name = "Essex Junction B&B"
                    },
                }
            };

            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(essex);
                Console.WriteLine("Essex Destination: {0}", context.Entry(essex).State);

                foreach (var lodging in essex.Lodgings)
                {
                    Console.WriteLine("{0}: {1}",
                                      lodging.Name,
                                      context.Entry(lodging).State);
                }
                context.SaveChanges();
            }
        }
Пример #9
0
 public void InsertsDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Destinations.Add(destination);
         context.SaveChanges();
     }
 }
 private static void UpdateLodging(Lodging lodging)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(lodging).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Пример #11
0
 private static void AttachDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(destination).State = EntityState.Unchanged;
         context.SaveChanges();
     }
 }
Пример #12
0
 // Serversidesimulation
 private static void AddDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Destinations.Add(destination);
         context.SaveChanges();
     }
 }
Пример #13
0
 private static void UpdateDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Entry(destination).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
 private static void UpdateTrip()
 {
     using (var context = new BreakAwayContext())
     {
         var trip = context.Trips.FirstOrDefault();
         trip.CostUSD = 750;
         context.SaveChanges();
     }
 }
Пример #15
0
 public static void UpdateTrip(int usd)
 {
     using (var context = new BreakAwayContext())
     {
         var trip = context.Trips.FirstOrDefault();
         trip.CostUSD = usd;
         context.SaveChanges();
     }
 }
Пример #16
0
 static void UpdatePersonDestination()
 {
     using (var context = new BreakAwayContext())
     {
         var destination = context.Destinations.FirstOrDefault();
         destination.Country = "Rowena";
         context.SaveChanges();
     }
 }
Пример #17
0
 public static void UpdatePerson()
 {
     using (var context = new BreakAwayContext())
     {
         var person = context.Persons.FirstOrDefault();
         person.FirstName = "Rowena";
         context.SaveChanges();
     }
 }
Пример #18
0
 public static void UpdateSocialSecurityNumber(int ssn)
 {
     using (var context = new BreakAwayContext())
     {
         var person = context.People.FirstOrDefault();
         person.SocialSecurityNumber = ssn;
         context.SaveChanges();
     }
 }
Пример #19
0
 private static void DeleteDestination(Destination destination)
 {
     using (var context = new BreakAwayContext())
     {
         context.Destinations.Attach(destination);
         context.Destinations.Remove(destination);
         context.SaveChanges();
     }
 }
Пример #20
0
        /// <summary>
        /// 这个功能演示联级删除 Annotation
        /// </summary>
        static void DeleteDestinationInMemoryAndDbCascade()
        {
            Guid destinationId;

            using (var context = new BreakAwayContext())
            {
                var destination = new AnnotationModel.Destination
                {
                    Name    = "Sample Destination",
                    Address = new AnnotationModel.Address
                    {
                        City          = "City",
                        StreetAddress = "StreetAddress",
                        State         = "State",
                        ZipCode       = "ZipCode"
                    },
                    Info = new AnnotationModel.PersonalInfo
                    {
                        DietryRestrictions = "DietryRestrictions",
                        Height             = new AnnotationModel.Measurement
                        {
                            Reading = 0.1M,
                            Units   = "0.2"
                        },
                        Width = new AnnotationModel.Measurement
                        {
                            Reading = 1.1M,
                            Units   = "1.2"
                        }
                    },
                    Lodgings = new List <AnnotationModel.Lodging>
                    {
                        new AnnotationModel.Lodging
                        {
                            Name = "Lodging One"
                        },
                        new AnnotationModel.Lodging
                        {
                            Name = "Lodging Two"
                        }
                    }
                };

                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }
            using (var context = new BreakAwayContext())
            {
                var destination = context.Destinations.Include("Lodgings").Single(d => d.DestinationId == destinationId);
                var aLodging    = destination.Lodgings.FirstOrDefault();
                context.Destinations.Remove(destination);
                Console.WriteLine("State of one Lodging: {0}", context.Entry(aLodging).State.ToString());
                context.SaveChanges();
            }
        }
 private static void SpecifyDatabaseName()
 {
     using (var context = new BreakAwayContext("BreakAwayStringConstructor"))
     {
         context.Destinations.Add(new Destination {
             Name = "Tasmania"
         });
         context.SaveChanges();
     }
 }
 private static void DeleteDestination(int destinationId)
 {
     using (var context = new BreakAwayContext())
     {
         var destination = new Destination {
             DestinationId = destinationId
         };
         context.Entry(destination).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
        private static void RemovePrimaryContact()
        {
            using (var context = new BreakAwayContext())
            {
                var davesDump = (from l in context.Lodgings
                                 where l.Name == "Dave's Dump"
                                 select l).Single();

                davesDump.PrimaryContactId = null;
                context.SaveChanges();
            }
        }
Пример #24
0
        //Deleting Existing Entities

        /* To delete an entity using Entity Framework, you use the Remove method on DbSet.
         * Remove works for both existing and newly added entities. Calling Remove on an entity
         * that has been added but not yet saved to the database will cancel the addition of the
         * entity. The entity is removed from the change tracker and is no longer tracked by the
         * DbContext. Calling Remove on an existing entity that is being change-tracked will register
         * the entity for deletion the next time SaveChanges is called.
         */

        private static void DeleteWineGlassBay()
        {
            using (var context = new BreakAwayContext())
            {
                var bay = (from d in context.Destinations
                           where d.Name == "Wine Glass Bay"
                           select d).Single();

                context.Destinations.Remove(bay);
                context.SaveChanges();
            }
        }
Пример #25
0
        //Changing Existing Entities
        private static void ChangeGrandCanyon()
        {
            using (var context = new BreakAwayContext())
            {
                var canyon = (from d in context.Destinations
                              where d.Name == "Grand Canyon"
                              select d).Single();

                canyon.Description = "227 mile long canyon.";
                context.SaveChanges();
            }
        }
Пример #26
0
 private static void UpdateTrip()
 {
     using (var context = new BreakAwayContext())
     {
         //var logger = new MyLogger();
         //context.Database.Log = s => logger.Log("EFApp", s);
         context.Database.Log = Console.Write;
         var trip = context.Trips.FirstOrDefault();
         trip.CostUSD = 750;
         context.SaveChanges();
     }
 }
Пример #27
0
        private static void DeleteGrandCanyon()
        {
            using (var context = new BreakAwayContext())
            {
                var canyon = (from t in context.Destinations
                              where t.Name == "Grand Canyon"
                              select t).Single();

                context.Destinations.Remove(canyon);
                context.SaveChanges();
            }
        }
Пример #28
0
 public void TearDown()
 {
     // Cleaning database
     using (var context = new BreakAwayContext())
     {
         var allDestinations = context.Destinations.ToList();
         foreach (var destination in allDestinations)
         {
             context.Destinations.Remove(destination);
         }
         context.SaveChanges();
     }
 }
Пример #29
0
        private static void CreatingNewProxies()
        {
            using (var context = new BreakAwayContext())
            {
                var nonProxy = new Destination()
                {
                    Name     = "Non-proxy Destination",
                    Lodgings = new List <Lodging>()
                };

                var proxy = context.Destinations.Create();
                proxy.Name = "Proxy Destinations";

                context.Destinations.Add(proxy);
                context.Destinations.Add(nonProxy);
                context.SaveChanges();

                var davesDump = (from l in context.Lodgings
                                 where l.Name == "Dave's Dump"
                                 select l).Single();

                context.Entry(davesDump)
                .Reference(l => l.Destination)
                .Load();

                Console.WriteLine("Before changes: {0}", davesDump.Destination.Name);

                nonProxy.Lodgings.Add(davesDump);

                Console.WriteLine("Added to non-proxy destination: {0}", davesDump.Destination.Name);

                proxy.Lodgings.Add(davesDump);

                Console.WriteLine("Added to proxy destination: {0}", davesDump.Destination.Name);

                context.SaveChanges();
            }
        }
Пример #30
0
        public static void InsertPerson()
        {
            var person = new Person
            {
                FirstName = "Rowan",
                LastName  = "Miller"
            };

            using (var context = new BreakAwayContext())
            {
                context.Persons.Add(person);
                context.SaveChanges();
            }
        }