Пример #1
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();
            }
        }
Пример #2
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();
            }
        }