Wednesday, 2 March 2016

Beginning-with-database-approach-Part 2


In our first part we have created our .edmx file with the help of ADO.net Entity Data Model using database approach.

Now, it’s time to start using our entity Framework to do SQL operation.

Before that,  if you are using visual studio 2012 or later, go to solution explorer, find your .edmx file and delete  .tt extension files from there.














After that go to .edmx.diagram file.
Right click > Properties > Code Generation Strategy= None . Change it to Default.











Now we are ready with our entity.




In entity framework, our tables behave as C# class and column as class properties.

Performing Insert operation using entity Framework


On some button click if we need to add that data to our database object, then we follows as  :

First Make an object of your entity, by default it should be as “your database name”Entitites.

Example :

db_OrderMangEntities db = new db_OrderMangEntities();

Now make object of table in which data is to be store,
Syntax :

TableName tbl = new TableName();

After that use this tbl instance to add data to the properties,

Example:

tbl_Vender tbl = new tbl_Vender();
            tbl.VenderName = "Kumar";
            tbl.VenderContact = 656789

When we have provided values to the properties to the table object, its time to save this into database for this,
Do,

db.tbl_Vender.AddObject(tbl);

Here, db is our entity object in which we have tbl_vendor (our table). The “AddObject”(or Add, depending upon your version) is the method provided by the entity to insert data into database.

After adding the data it’s time to tell our database to save our changes, for this write,

db.SaveChanges();


This will add a row to our table with the provided data.

Complete code will look like :


protected void btnRegister_Click(object sender, EventArgs e)
        {
            db_OrderMangEntities db = new db_OrderMangEntities();
            tbl_Vender tbl = new tbl_Vender();
            tbl.VenderName = "Kumar";
            tbl.VenderContact = 656789;

            db.tbl_Vender.AddObject(tbl);
            db.SaveChanges();


        }


Update Object in Entity:


To update any row, we need to first find that row.
For that we have to use LINQ with enitites.
The Syntax will be :


tbl_Vender tbl1 = db.tbl_Vender.Where(i => i.VenderName == "Kumar").First();


Here I am searching with vendorName you can use any property.
After this we will have tbl1 as the object of that row which is to be updated.
Now assign the new values to the tbl1 object.

For Example :

tbl1.VenderName = "Gaurav";

After this just tell the database to save our changes.

db.SaveChanges();

Note , here we are not adding another object to the table as it will add another row, but we just need to update it.


 Deleting object:

Same first we need to find the row to be deleted .

Now write,

db.tbl_Vender.DeleteObject(tbl1);

and then the same save changes to database.


Beginning-with-database-approach-part-3


No comments:

Post a Comment