static int CreateModel() { int modelId = 0; using (var db = new BikeStoreEntities()) { //approach #1 sproc through the function import /* var modelIdParam = new System.Data.Entity.Core.Objects.ObjectParameter("modelid", -1); var model = new Model { Name = "Domane 5.2", ListPrice = 3499.99m }; //modeid is an output param var result = db.ModelInsert(modelIdParam, model.Name, null, null, null, null, null, null, model.ListPrice,null,null,null); modelId = (int)modelIdParam.Value; */ //approach 2 Used Stored procedure mapping on the Model entity //Need to have a select @ the end of the inset sproc var model = new Model { Name = "Domane 5.2", ListPrice = 3499.99m }; db.Models.Add(model); db.SaveChanges(); modelId = model.ModelId; } Console.WriteLine("New Model: " + modelId); return modelId; }
static void DeleteModel(int modelId) { using (var db = new BikeStoreEntities()) { // uses sprocs when mapping set in model var model = new Model { ModelId = modelId }; db.Models.Attach(model); db.Models.Remove(model); db.SaveChanges(); } }
static void UpdateModel(int modelId) { using (var db = new BikeStoreEntities()) { //ModelSelectByKey mapped to entity "Model" var model = db.ModelSelectByKey(modelId).FirstOrDefault(); if (model != null) { //the following is necessary if the return type of ModelSelectByKey is complex type ModelSelectByKey_Result // ModelSelectByKey uses the default /* Model m = new Model(); m.ModelId = model.ModelId; m.Name = model.Name; m.ManufacturerCode = model.ManufacturerCode; m.CategoryId = model.CategoryId; m.Description = model.Description; m.Features = model.Features; m.StatusId = model.StatusId; m.ManufacturerId = model.ManufacturerId; m.ListPrice = model.ListPrice; m.ImageCollection = model.ImageCollection; m.CategoryCustomData = model.CategoryCustomData; m.ManufacturerCustomData = model.ManufacturerCustomData; db.Models.Attach(m);*/ // make the change - needs to happen after the attach, otherwise the change // will not be registered //m.Features = "500 Series OCLV Frame"; model.Features = "500 Series OCLV Frame"; db.SaveChanges(); } } }