Monday, January 4, 2010

Cause Of Face Swelling Up

Entity Framework From The Beginning (Part II) Entity Framework

Interacting with the database

"Where is the 'any' key?"
- Homer Simpson, in response to the message, "Press any key"

In this second part of the tutorial on Entity Framework 4.0, will focus on as persisitir the changes I make in the model (EDM) in the associated database. To do this we will use a new example that may downloader following the link at the bottom of the post. As we have seen in the first post of this series, retrieve data from the database, now we will see the persistence of new objects, the modification of existing institutions and the elimination of these.

Let

EDM in the figure raised for this example:

DemoEFParte2EDM

Now let's create some new objects and try to make these changes impact on our database as appropriate, in the following list of how we see a ejmplo do so.

 
class Program {
static void Main (string [] args) {

using (CopyRentADMEntities CopyRentADMEntities ctx = new ()) {

/ / I think some new brands
MarcasEquipo MarcasEquipo m1 = new ();
m1. Brand = 1;
m1.Nombre = "Perox"
MarcasEquipo MarcasEquipo m2 = new ();
m2.MarcaID = 2;
m2.Nombre = "Sister";
m2.Web = "http://www.Sister.net"
MarcasEquipo MarcasEquipo m3 = new ();
m3.MarcaID = 3;
m3 . Name = "Sonica-Pinolta"
m3.Web = "http://www.Sonica-Pinolta.com"
/ / Add context-Marks Created
ctx.MarcasEquipo.AddObject (M1) ctx
. AddToMarcasEquipo (m2);
ctx.AddToMarcasEquipo (m3);
/ / persist changes
ctx.SaveChanges ();}

System.Console.WriteLine ("Wait for user entry!");
System.Console.ReadLine (),
}}


If we went now SQL Server Management Studio and do a query on the table that just entered the new data we can see how they were kept successfully in database, as seen in the picture below.



DemoEFParte2SQLMgmConfirm



's fill some data from the other entities of the model with the following list.



 
class Program {
static void Main (string [] args) {

using (CopyRentADMEntities CopyRentADMEntities ctx = new ()) {

/ / I think some Models
ModelosEquipos ModelosEquipos mo1 = new ();
ModelosEquipos mo2 ModelosEquipos = new ();
ModelosEquipos mo3 ModelosEquipos = new ();
ModelosEquipos Mo4 ModelosEquipos = new ();
ModelosEquipos ModelosEquipos MO5 = new ();
ModelosEquipos MO6 = ModelosEquipos new ();
/ / Setting properties
mo1.MarcaID = 1;
mo1.ModeloID = 1;
mo1.NombreModelo = "MK2000";
mo2.MarcaID = 2;
mo2.ModeloID = 2;
mo2. NombreModelo = "Vison208;
mo3.MarcaID = 2;
mo3.ModeloID = 3;
mo3.NombreModelo = "Printer3";
mo4.MarcaID = 3;
mo4.ModeloID = 4;
mo4.NombreModelo = "CR100";
mo5.MarcaID = 3;
mo5.ModeloID = 5;
mo5.NombreModelo = "Comodo64";
mo6.MarcaID = 3;
mo6.ModeloID = 6;
mo6.NombreModelo = "Ataque_77";
//Add objects to context
ctx.AddToModelosEquipos(mo1);
ctx.AddToModelosEquipos(mo2);
ctx.AddToModelosEquipos(mo3);
ctx.AddToModelosEquipos(mo4);
ctx.AddToModelosEquipos(mo5);
ctx.AddToModelosEquipos (MO6)
/ / Saving Changes
ctx.SaveChanges ();
/ / I think some new
Equipment Equipment Equipment e1 = new ();
Equipment Equipment e3 = new ();
e2 = new Equipment Equipment ();
Equipment Equipment e4 = new ();
/ / Seting Properties
e1.EquipoID = 1;
e1.Medidor = 23399;
e1.ModeloID = 1;
e1.Propio = true;
e1.Tension = true;
e2.EquipoID = 2;
e2.ModeloID = 2;
e2.Propio = false;
e2.Tension = false;
e3.EquipoID = 3;
e3.ModeloID = 5;
e3.Propio = true;
e3.Tension = false;
e4.EquipoID = 4;
e4.ModeloID = 5;
e4.Propio = true;
e4 . Tension = true;
/ / Adding Equipment to context
ctx.AddToEquipos (e1);
ctx.AddToEquipos (e2);
ctx.AddToEquipos (e3);
ctx.AddToEquipos (e4);
/ / Saving Changes
ctx.SaveChanges ();

}}}


Let us see how to edit the elements



 / / Making Some Changes 
/ / Selecting the elements
SelEquipos var = from p in ctx.Equipos
WHERE p.ModeloID
== 5 select p;
foreach (Equipment and in SelEquipos)
e.Medidor = 1000;
/ / Saving Changes
ctx.SaveChanges ();


And now remove an item:



 / / Deleting an Object 
SelEquiposToDelete var = from p in ctx.Equipos

WHERE p.EquipoID == 2 select p;
ctx. DeleteObject (SelEquiposToDelete.First ());
/ / Saving Changes
ctx.SaveChanges ();




Conclusion These first two post to serve as an introduction to using the Entity Framework 4.0 covered the basic aspects about the tool as follows:




  • Connect to a database and obtain a model (EDM) through the wizard.


  • Get the context to operate on the entities of the model and then the changes persist in the database. Make


  • CRUD operations.



In the next post we will see one of the most anticipated features of EF 4 and is the support POCO (Plain Old CLR Objects). From this perspective we can appreciate the true power of the Entity Framework.



Happy Programming!

0 comments:

Post a Comment