Showing posts with label Entity framework. Show all posts
Showing posts with label Entity framework. Show all posts

Tuesday, 8 March 2016

Beginning-with-database-approach-Part 3

0

In Part 2 of beginning with entity framework we did CRUD operation using entity framework. Now, there are few tips for using entity:


How to update your Entity Data Model (.edmx)?


Open your .edmx file >Right click >Select update from database
You will get this window:






















If you have added new table, views, stored procedure in database.. Select those and select Finish.
Same to update and delete from model.

Providing Default values?


In SQL we can provide default value, to a column, this default value is not automatically updated to our entity model. So we have to manual add the default value in our entity model also.
Select the table column from entity designer for which default value is to be provided.

Right click on the column>Properties >Default value :- Provide your value

























#Stored procedure of SQL behave as function in Entity framework.






Read More »

Wednesday, 2 March 2016

Beginning-with-database-approach-Part 2

0

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


Read More »

Tuesday, 1 March 2016

Beginning with database approach Part-1

0



This approach could be used with any previously build database, that why known as database model approach. It will help us to communicate with our database, with its predefined functions, and giving our C# code more power in terms of SQL too.


Step 1: Right- Click on the project main file.

Project Name > Add > New Item > ADO.NET Entity Data Model



Provide a relevant Name.

Step 2: Choose Modal Contents :





Select Generate from database > then Next >


Step 3:  : Make a new Connection to connect to database : (connection string)





Provide your database server name and select the database name, that you want to connect.
Click Next.

Step 4:  Select the table, views, stored procedure that are required from database, or select all.




Click > Finish.



Your entity Model is created:

You should have got your database tables in GUI format as below :





Read More »

Monday, 28 December 2015

Entity Framework Development Approaches

0

This article is taken from msdn.microsoft.com, it was a necessary part so i provided the content here also.


The Entity Framework is the easiest ORM framework to learn for an ASP.NET developer. Because Microsoft continues to focus ORM development efforts on the Entity Framework, Microsoft and third parties continue to produce new tutorials, videos, and books. Most new tutorials use the Entity Framework even if they are focused on some other technology, such as the latest release of MVC or new model binding features for Web Forms in ASP.NET 4.5.


There are three ways you can work with data models and databases in the Entity Framework:Database FirstModel First, and Code First.



  • Database First
    If you already have a database, the Entity Framework designer built into Visual Studio can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. The information about your database structure (store schema), your data model (conceptual model), and the mapping between them is stored in XML in an .edmx file. The Entity Framework designer provides a graphical UI that you can use to display and edit the .edmx file.
  • Model First
    If you don't have a database yet, you can begin by creating a model in an .edmx file by using the Entity Framework graphical designer in Visual Studio. When the model is finished, the Entity Framework designer can generate DDL (data definition language) statements to create the database. As in Database First, the .edmx file stores model and mapping information.
  • Code First
    Whether you have an existing database or not, you can use the Entity Framework without using the designer or an .edmx file. If you don't have a database, you can code your own classes and properties that correspond to tables and columns. If you do have a database, Entity Framework tools can generate the classes and properties that correspond to existing tables and columns. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you let Code First create the database, you can use Code First Migrations to automate the process of deploying the database to production. Migrations can also automate the deployment of database schema changes to production when your data model changes.

Choose Code First for new development unless you want to use a graphical designer to model database objects and relationships. The Entity Framework designer only works with Database First and Model First. Before you choose Database First or Model First, however, consider how you want to handle updates to the data model after you create the database, and how you want to deploy the database and deploy updates to it. Code First Migrations automates the process of implementing and deploying database schema changes that result from data model changes. The advantages of Code First Migrations might outweigh the advantages of the Entity Framework designer.

Read More »

Thursday, 3 December 2015

How to bind dropdown with multiple columns using entity framework?

0


Problem:

We have to bind the table to the dropdown where we need the complete name as the text value of dropdown and emp_id as datavalue.



The name of dropdownlist is ddlRequestTo and the entity database object named context with tbl_employeePersonal


Solution:

private void bindRequestTo()

{
ddlRequestTo.DataSource = (from emp in context.tbl_employeePersonal select new { FullName = emp.emp_firstName + " " + emp.emp_middleName + " " + emp.emp_lastName, emp.emp_Id}).ToList();
ddlRequestTo.DataTextField = "FullName";
ddlRequestTo.DataValueField = "emp_Id";
ddlRequestTo.DataBind();
ddlRequestTo.Items.Insert(0, "--Send Request To--");
}




Read More »

Sunday, 29 November 2015

How to Bind Dropdownlist in Asp.net using entity framework

0

Question :

How can we bind dropdown list in Asp.net from a table (tbl_department) using entity framework where tbl_department consist of departmentId and departName ?


Solution

Designer.aspx

<asp:DropDownList ID="ddlDepartment" runat="server" AutoPostBack="true">

</asp:DropDownList>


code.cs


db_EmployeeManagEntities context = new db_EmployeeManagEntities();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDepartment();
}
}

private void bindDesignation()
{
var designation = from d in context.tbl_designation select new { d.designationId, d.designationName };
ddlDesignation.DataSource = designation;
ddlDesignation.DataTextField = "designationName";
ddlDesignation.DataValueField = "designationId";
ddlDesignation.DataBind();
ddlDesignation.Items.Insert(0, " --Select Designation--");
}




Read More »