Pro Entity Framework 4.0 - Apress_3
Số trang: 26
Loại file: pdf
Dung lượng: 1.15 MB
Lượt xem: 13
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
đến nay trong chương này, bạn đã làm việc với các thực thể duy nhất mà không cần đối phó với bất kỳ của các hiệp hội hoặc các mối quan hệ của họ. Trong ví dụ trước, bạn đã cập nhật hoặc chèn vào các bảng hoạt động trong một vai trò cha mẹ, chẳng hạn như ProductModel và người.
Nội dung trích xuất từ tài liệu:
Pro Entity Framework 4.0 - Apress_3 CHAPTER 5 ■ WORKING WITH ENTITIES In this example, you use the CreateProductModel method to create a new ProductModel object. Thismethod lets you specify the property values in the method overload. Then, as in the previous example,you add that object to the ProductModel using the AddToProductModel method.Relational InsertsSo far in this chapter, you’ve worked with single entities without dealing with any of their associations orrelationships. In the previous examples, you’ve updated or inserted into tables that act in a parent role,such as ProductModel and Person. But in reality, developers work with relational data, and that meansworking with child entities. Product suppliers may add product models on occasion, but they addrelated products much more often. The EF needs to be able to insert related child data easily.Fortunately, it does this quite well. Let’s illustrate this functionality with an example. For this example, add another button to your form, and add the following code to the button’s Clickevent:try{ using (var context = new AdventureWorks2008Entities()) { var prodMod = context.ProductModels.Where(pm => pm.ProductModelID == 129).First(); var prod = new Product(); prod.Name = Inverted Kayaba; prod.ProductNumber = IKAYA-R209; prod.MakeFlag = true; prod.FinishedGoodsFlag = true; prod.Color = Red; prod.SafetyStockLevel = 250; prod.ReorderPoint = 250; prod.StandardCost = 2500; prod.ListPrice = 3900; prod.Size = 40M; prod.SizeUnitMeasureCode = CM; prod.WeightUnitMeasureCode = LB; prod.Weight = (decimal)45.2; prod.DaysToManufacture = 5; prod.ProductLine = S; prod.Class = M; prod.Style = M; prod.ProductSubcategoryID = 1; prod.SellStartDate = DateTime.Now; prod.ModifiedDate = DateTime.Now; prod.rowguid = Guid.NewGuid(); prod.ProductModel = prodMod; context.SaveChanges(); label1.Text = Save Successful; }}catch (Exception ex){ MessageBox.Show(ex.InnerException.Message);} 89CHAPTER 5 ■ WORKING WITH ENTITIES In this example, a new Product is created in memory and then attached to the related ProductModel that was queried and returned from the data store. After it’s attached, the SaveChanges method is called. Prior to running the example, open SQL Server Profiler again so you can evaluate the query that is executed. Run the project, and click the new button when the form displays. As in the previous examples, the label displays the success message after the code executes successfully. In SSMS, execute the following query: SELECT * FROM Production.Product ORDER BY ProductModelID Scroll down to the bottom of the Results window, and you see the newly added row, shown in Figure 5-3. Figure 5-3. Relational insert In this example, you first query for the ProductModel you want to attach the Product to. You then create a new instance of the Product class and fill in its properties. You attach the new Product to the ProductModel. However, look at the code that creates the new Product. After the new product is created in memory, it’s attached to the ProductModel, but where is the relation? If you look at the table in SSMS, you see a foreign key column called ProductModelID; but it isn’t set in the previous code. If you query the Product table for the record that was just inserted, it does have the correct ProductModelID value. Go back to SQL Server Profiler, and find the INSERT statement. I’ve included it here as well. Notice that the ProductModelID column is included in this T-SQL statement with the correct value: exec sp_executesql Ninsert [Production].[Product]([Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], [Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], [SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], [Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], [DiscontinuedDate], [rowguid], [ModifiedDate]) values (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, null, null, @20, @21) select [ProductID] from [Production].[Product] where @@ROWCOUNT > 0 and [ProductID] = scope_identity(),N@0 nvarchar(50),@1 nvarchar(25),@2 b ...
Nội dung trích xuất từ tài liệu:
Pro Entity Framework 4.0 - Apress_3 CHAPTER 5 ■ WORKING WITH ENTITIES In this example, you use the CreateProductModel method to create a new ProductModel object. Thismethod lets you specify the property values in the method overload. Then, as in the previous example,you add that object to the ProductModel using the AddToProductModel method.Relational InsertsSo far in this chapter, you’ve worked with single entities without dealing with any of their associations orrelationships. In the previous examples, you’ve updated or inserted into tables that act in a parent role,such as ProductModel and Person. But in reality, developers work with relational data, and that meansworking with child entities. Product suppliers may add product models on occasion, but they addrelated products much more often. The EF needs to be able to insert related child data easily.Fortunately, it does this quite well. Let’s illustrate this functionality with an example. For this example, add another button to your form, and add the following code to the button’s Clickevent:try{ using (var context = new AdventureWorks2008Entities()) { var prodMod = context.ProductModels.Where(pm => pm.ProductModelID == 129).First(); var prod = new Product(); prod.Name = Inverted Kayaba; prod.ProductNumber = IKAYA-R209; prod.MakeFlag = true; prod.FinishedGoodsFlag = true; prod.Color = Red; prod.SafetyStockLevel = 250; prod.ReorderPoint = 250; prod.StandardCost = 2500; prod.ListPrice = 3900; prod.Size = 40M; prod.SizeUnitMeasureCode = CM; prod.WeightUnitMeasureCode = LB; prod.Weight = (decimal)45.2; prod.DaysToManufacture = 5; prod.ProductLine = S; prod.Class = M; prod.Style = M; prod.ProductSubcategoryID = 1; prod.SellStartDate = DateTime.Now; prod.ModifiedDate = DateTime.Now; prod.rowguid = Guid.NewGuid(); prod.ProductModel = prodMod; context.SaveChanges(); label1.Text = Save Successful; }}catch (Exception ex){ MessageBox.Show(ex.InnerException.Message);} 89CHAPTER 5 ■ WORKING WITH ENTITIES In this example, a new Product is created in memory and then attached to the related ProductModel that was queried and returned from the data store. After it’s attached, the SaveChanges method is called. Prior to running the example, open SQL Server Profiler again so you can evaluate the query that is executed. Run the project, and click the new button when the form displays. As in the previous examples, the label displays the success message after the code executes successfully. In SSMS, execute the following query: SELECT * FROM Production.Product ORDER BY ProductModelID Scroll down to the bottom of the Results window, and you see the newly added row, shown in Figure 5-3. Figure 5-3. Relational insert In this example, you first query for the ProductModel you want to attach the Product to. You then create a new instance of the Product class and fill in its properties. You attach the new Product to the ProductModel. However, look at the code that creates the new Product. After the new product is created in memory, it’s attached to the ProductModel, but where is the relation? If you look at the table in SSMS, you see a foreign key column called ProductModelID; but it isn’t set in the previous code. If you query the Product table for the record that was just inserted, it does have the correct ProductModelID value. Go back to SQL Server Profiler, and find the INSERT statement. I’ve included it here as well. Notice that the ProductModelID column is included in this T-SQL statement with the correct value: exec sp_executesql Ninsert [Production].[Product]([Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], [Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], [SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], [Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], [DiscontinuedDate], [rowguid], [ModifiedDate]) values (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, null, null, @20, @21) select [ProductID] from [Production].[Product] where @@ROWCOUNT > 0 and [ProductID] = scope_identity(),N@0 nvarchar(50),@1 nvarchar(25),@2 b ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 317 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 289 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 233 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 207 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 204 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 204 0 0