Beispiel #1
0
 static void FluentApiUpdatePerson()
 {
     using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
     {
         var person = context.Persons.FirstOrDefault();
         person.FirstName = "Rowena";
         context.SaveChanges();
     }
 }
Beispiel #2
0
 static void FluentApiUpdatePersonDestination()
 {
     using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
     {
         var destination = context.Destinations.FirstOrDefault();
         destination.Country = "Rowena";
         context.SaveChanges();
     }
 }
Beispiel #3
0
        /// <summary>
        /// 这个功能演示联级删除 FluentApi
        /// </summary>
        static void FluentApiDeleteDestinationInMemoryAndDbCascade()
        {
            Guid destinationId;

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

                context.Destinations.Add(destination);
                context.SaveChanges();
                destinationId = destination.DestinationId;
            }
            using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
            {
                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();
            }
        }
Beispiel #4
0
        /// <summary>
        /// 由EF框架生成的INSERT语句会将字符串 "Resort" 放进新加入行的Discriminator列中
        /// </summary>
        static void FluentApiInsertResort()
        {
            //创建Resort类型的实例
            var resort = new FluentApiModel.Resort
            {
                Name = "Top Notch Resort and Spa",
                MilesFromNearestAirport = 30,
                Activities  = "Spa, Hiking, Skiing, Ballooning",
                Destination = new FluentApiModel.Destination
                {
                    Name    = "Stowe, Vermont",
                    Country = "USA",
                    Address = new FluentApiModel.Address
                    {
                        City = "City",
                    },
                    Info = new FluentApiModel.PersonalInfo
                    {
                        DietryRestrictions = "DietryRestrictions",
                        Width = new FluentApiModel.Measurement
                        {
                            Reading = 1M,
                            Units   = "Units"
                        },
                        Height = new FluentApiModel.Measurement
                        {
                            Reading = 2M,
                            Units   = "Units2"
                        }
                    }
                }
            };

            using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
            {
                //并且要保存到 Lodgings 它的基类中
                //EF框架将会在Discriminator列中插入字符串 "Resort".
                context.Lodgings.Add(resort);
                context.SaveChanges();
            }
        }
Beispiel #5
0
        //static void FluentApiInsertPerson()
        //{
        //    var person = new FluentApiModel.Person
        //    {
        //        FirstName = "Rowan",
        //        LastName = "Miller",
        //        SocialSecurityNumber = 12345678
        //    };
        //    using (var context = new FluentApiBreakAwayContext())
        //    {
        //        context.Persons.Add(person);
        //        context.SaveChanges();
        //    }
        //}

        /// <summary>
        /// 映射到继承层次结构
        /// 由EF框架生成的INSERT语句会将字符串 "Lodging" 放进新加入行的Discriminator列中
        /// </summary>
        static void FluentApiInsertLodging()
        {
            var lodging = new FluentApiModel.Lodging
            {
                Name        = "Rainy Day Motel",
                Destination = new FluentApiModel.Destination
                {
                    Name    = "Seattle, Washington",
                    Country = "USA",
                    Address = new FluentApiModel.Address
                    {
                        City = "City",
                    },
                    Info = new FluentApiModel.PersonalInfo
                    {
                        DietryRestrictions = "DietryRestrictions",
                        Width = new FluentApiModel.Measurement
                        {
                            Reading = 1M,
                            Units   = "Units"
                        },
                        Height = new FluentApiModel.Measurement
                        {
                            Reading = 2M,
                            Units   = "Units2"
                        }
                    }
                }
            };

            using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
            {
                context.Lodgings.Add(lodging);
                context.SaveChanges();
            }
        }
Beispiel #6
0
        static void FluentApiInsertDestination()
        {
            var destination = new FluentApiModel.Destination
            {
                Country     = "Indonesia",
                Description = "EcoTourism at its best in exquisite Bali",
                Name        = "Bali",
                Address     = new FluentApiModel.Address
                {
                    City          = "shanghai",
                    State         = "huayi",
                    ZipCode       = "000000",
                    StreetAddress = "yishanlu"
                },
                Info = new FluentApiModel.PersonalInfo
                {
                    DietryRestrictions = "DietryRestrictions",
                    Height             = new FluentApiModel.Measurement
                    {
                        Reading = 100,
                        Units   = "200"
                    },
                    Width = new FluentApiModel.Measurement
                    {
                        Reading = 200,
                        Units   = "300"
                    },
                },
                Lodgings = new List <FluentApiModel.Lodging>()
                {
                    new FluentApiModel.Lodging
                    {
                        Name  = "lodging Name",
                        Owner = "lodging Owner",
                        //IsResort = true,
                        MilesFromNearestAirport = 1.1M
                    },
                    new FluentApiModel.Lodging
                    {
                        Name  = "lodging Name2",
                        Owner = "lodging Owner2",
                        //IsResort = true,
                        MilesFromNearestAirport = 2.2M
                    }
                }
            };


            using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
            {
                context.Destinations.Add(destination);
                context.SaveChanges();
            }

            using (var context = new FluentApiBreakAwayContext(nameOrConnectionString))
            {
                var destinationsArray = context.Destinations.ToList();
                var destinationFirst  = destinationsArray[0];
                destinationFirst.Description += "Trust us, you'll love it!";
                context.SaveChanges();
                //context.Destinations.Remove(destinationFirst);
                context.SaveChanges();
            }
        }