static void DeleteDestinationInMemoryAndDbCascade2() { //NOTE: //随同Destination一起删除以前存入的Loading数据。我们删除与Lodging提到的所有相关代码。 //由于内存中无Lodging,就不会有客户端的级联删除,而数据库却清除了任何孤立的Lodgings数据, //这是因为在数据库中定义了级联删除。 Guid destinationId; using (var context = new BreakAwayContext()) { var destination = new AnnotationModel.Destination { Name = "Sample Destination", Address = new AnnotationModel.Address { City = "City" }, Lodgings = new List <AnnotationModel.Lodging> { new AnnotationModel.Lodging { Name = "Lodging One" }, new AnnotationModel.Lodging { Name = "Lodging Two" } }, Info = new AnnotationModel.PersonalInfo { DietryRestrictions = "DietryRestrictions", Width = new AnnotationModel.Measurement { Reading = 2.1M, Units = "Units" }, Height = new AnnotationModel.Measurement { Reading = 3.1M, Units = "Units2" } } }; context.Destinations.Add(destination); context.SaveChanges(); destinationId = destination.DestinationId; } using (var context = new BreakAwayContext()) { var destination = context.Destinations.Single(d => d.DestinationId == destinationId); context.Destinations.Remove(destination); context.SaveChanges(); } using (var context = new BreakAwayContext()) { var lodgings = context.Lodgings.Where(l => l.DestinationId == destinationId).ToList(); Console.WriteLine("Lodgings: {0}", lodgings.Count); } }
/// <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(); } }
static void InsertDestination() { var destination = new AnnotationModel.Destination { Country = "Indonesia", Description = "EcoTourism at its best in exquisite Bali", Name = "Bali", Address = new AnnotationModel.Address { City = "shanghai", State = "huayi", ZipCode = "000000", StreetAddress = "yishanlu" }, 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" } } }; using (var context = new BreakAwayContext()) { context.Destinations.Add(destination); context.SaveChanges(); } }