Ugrás a tartalomhoz

.NET Programming Technologies

Gergely Kovásznai, Csaba Biró

Eszterházy Károly College

Data Manipulations

Data Manipulations

In LINQ to XML, it is not supported to insert new records into our XML files, or to update or modify existing ones. SQL databases, however, offer such a support, and so do ORM tools; including LINQ to Entities, whose corresponding service is extremely simple to use. Roughly speaking, the framework registers and collects all the manipulations on entity classes, and you can eventually decide to instruct the framework, by calling a special method, to flush those manipulations into the database. This method is SaveChanges()in the „context” class.

It does not take any explaining how to perform updates; simply modify the properties of the entity classes!

Chocolat choco;

...

choco.Name = "Twix";

choco.Manufacturer = (

                        from m in App.db.Manufacturers

                        where m.Name == "Mars, Inc."

                        select m

                        ).First();

...            

App.db.SaveChanges();

All the changes in the database can be tracked by using Visual Studio’s Server Explorer. However, pay attention! Changes are made not in the original SDF file, but rather on its copy, which is automatically copied to the bin directory of our project every time when compiling. Consequently, if you would like to track database changes in Server Explorer, open the SDF file in the bin directory!

It is also self-explanatory how to perform inserts: create a new entity object, and add it to the appropriate collection in the „context” object! There is, however, a special case of insertion, which is shown in the second half of the code below: a new record gets inserted into a hidden junction table (ChocolatMaterial) behind the scenes, if you add an object (mat) to the appropriate collection (Materials) in an instance (choco) of one of the joint tables.

Material mat = new Material

{

        Name = "milk",

        UnitName = "liter",

        UnitPrice = 1.1

};

App.db.Materials.Add(mat);

...

Chocolat choco = (

                        from ch in App.db.Chocolats

                        where ch.Name == "Milka Alpine Milk"

                        select ch

                        ).First();

choco.Materials.Add(mat);

...

App.db.SaveChanges();

It is also easy to perform deletes by deleting from appropriate collections. In the first half of the code below, one of the ingredients of a chocolat is deleted, which implies deleting a record from the hidden junction table (ChocolatMaterial). In the second half of the example, we would like to delete a (global) chocolat object, but beforehand we need to empty the collections in the object as well (Materials). If you miss this step, you can easily get an error message (if relations between tables have been properly declared in the database).[21]

Chocolat choco = (

                        from ch in App.db.Chocolats

                        where ch.Name == "Rum Kokos"

                        select ch

                        ).First();

choco.Materials.Remove((

                        from m in choco.Materials

                        where m.Name == "rum"

                        select m

                        ).First());

...

choco.Materials.Clear();

App.db.Chocolats.Remove(choco);

...

App.db.SaveChanges();



[21] A lot of database management systems and ORM tools support the so-called cascade delete, which means the following: when a record is deleted, all the records connected to this one are automatically deleted as well. This setting is database-dependent, and, furthermore, is only partly supported by LINQ to Entities (Freeman & Rattz, 2010).