Insert Database First Approach (Entity Framework)

First we need to project into visual studio and give it any name . To work with entity framework we also need to add entity framework package into project . To add the package first right click the then select MangeNuget Package option then it will pop up the window . Search for entity framework package .


 Select entity framework package then click install . It will install the package . We will use windows from application for insert , update , delete and retrieve operation  . To keep this example simple i decided to use simple table for insert username and password . Here is the script to create the table .Make you define the primary key otherwise model will throw error failed to mapped .

CREATE TABLE [dbo].[Cashir](
[User_Id] [int] IDENTITY(1,1) NOT NULL,
[username] [nvarchar](50) NOT NULL,
[password] [nvarchar](50) NOT NULL,
)

Then generate model from database . to do that we have to following steps .Add the ADO.NET model into project . Right click the on the project then select data tap . it will the ado.net data model option . click the add button with a name .


After click the add button we will see the approach we want to use  .

Select first option generate model from database then click the next button .

Enter the sql server name and type of authentication you are using . We also need to select the database name where we created the tables then click ok button . Then we will see the window for selecting database and the tables then select the table name and click finish .



then you will se model into visual studio which is auto generated  .We can see that in app.config file a connection string is also added . 

Then add a form look like this ..drag and drop labels and button on the form .



double click the save to generate event handler . copy and paste following code .


      private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" )
            {
                MessageBox.Show("Please provide information");
                return;
            }
            else
            {

                Cashir ca = new Cashir();
                ca.username = textBox1.Text;
                ca.password = textBox2.Text;
                bool result = SaveUserDetails(ca);
                label3.Text = "User Details is inserted ";

            }

        }

        public bool SaveUserDetails(Cashir ca)
        {
           
                bool result = false;
                using (UserEntities user_entity = new UserEntities())
                {
                    user_entity.Cashirs.Add(ca);
                    user_entity.SaveChanges();

                    result = true;
                }
                return result;
            }
           
        }

    }


if you done everything correctly we will be able to insert new record .. Next tutorial we will discuss how update the records .











No comments:

Post a Comment