VistaDB 5
LINQ Script CRUD Examples

Right clicking on an entity in the LINQ Entity Model tab will present a number of options for scripting. Example are below.

Select Entity

This is what a default Script to Select looks like for the orderdetails table.

Here is an example of the same select statement, but modified to do a projection of the order detail and the parent order date.

Select Entity
Copy Code
ScratchModel.ScratchEntities context = new ScratchModel.ScratchEntities(connection);
var query = from i in context.OrderDetails select new { i.OrderID, i.Orders.OrderDate };
return query; 

Update Entity

Update an entity requires you first load the entity with some sort of where clause (you must provide filter value).

Update Entity
Copy Code
ScratchModel.ScratchEntities context = new ScratchModel.ScratchEntities(connection);
context.Orders.Select(i => i).Where('Provide filter case').FirstOrDefault().'Provide member' = 'value';
context.SaveChanges();
return "Done"; 

Create Entity

Create adds a new object, and puts it to the context, then saves it. Only the defaults have been set at this point. To do an actual insert you would put your own object specific members where the comment line is inserted.

Create Entity
Copy Code
ScratchModel.ScratchEntities context = new ScratchModel.ScratchEntities(connection);
ScratchModel.Orders i = new ScratchModel.Orders();
//Set object specific members here
context.AddToOrders(i);
context.SaveChanges();
return "Done"; 

Delete Entity

To delete an entity from an EF model you call DeleteObject with the object selected (or you can get the object as shown in the sample).
You still have to call SaveChanges to commit the changes back to the database.

Delete Entity
Copy Code
ScratchModel.ScratchEntities context = new ScratchModel.ScratchEntities(connection);
context.DeleteObject(context.Orders.Select(i => i).Where('Provide delete case').FirstOrDefault());
context.SaveChanges();
return "Done"; 
See Also