Đang chuẩn bị liên kết để tải về tài liệu:
[FSC]Apress Pro Entity Framework 4.0_5
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
In this example, you use the CreateProductModel method to create a new ProductModel object. This method 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. | CHAPTER 5 WORKING WITH ENTITIES In this example you use the CreateProductModel method to create a new ProductModel object. This method 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 Inserts So far in this chapter you ve worked with single entities without dealing with any of their associations or relationships. 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 means working with child entities. Product suppliers may add product models on occasion but they add related 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 Click event 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 89 CHAPTER 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