Insert Operation in Model First Approach

First we need to add empty model into project . To this right click on the project and add the ado.net model into project then select the empty model into project .




Enter your server name with authentication mode. If you want to create new database with name or create table with old database both is possible . you can choose either one . then click ok . the database will be created with provided name . then next step you will see the all the sql script is generated for you   



On the empty model right click on the project then new window will appear select add new entity option . On this window give a name of your Entity with Id inter 32 bit then click.Then you will see somethings like this ...Then add two more properties like username and password  etc ..






Then click the right on the model and then select generate database from model option then new will appear ..


From this window select finish option the you will see inside  sql management studio  a database is generated with the name you provided .  then it will generate the sql script for you as well into edmx.sql file . open this file then select the option execuate option. After execute the query . Check sql Management studio again and refresh it you will see the table is also generated . Do not forget to add the entity frame work package into your project . Then add console application into your project .  

This is same approach we used on our last tutorial (DB First Approach). First design normal interface where user can select the following options  like this ..


Then copy and paste following  code ..


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


            Console.WriteLine("Please enter your choice");
           
            Choice();
            Console.ReadLine();



        }
        static int count, mnuChoice = 0;// count noof trials for login if > 3 exit user!! 
        public static int UI_main()
        {

            Console.WriteLine("\tWelcome: ");
            Console.WriteLine("1.Insert.\t\t2.Update.\n");
            Console.WriteLine("3.Delete.\t\t4.User Record.\n");



            Console.Write("Enter your choice: ");
            try
            {
                int ch = int.Parse(Console.ReadLine());
                return ch;
            }
            catch (Exception)
            {
                return -1;
            }

        }

        public static void Choice()
        {

            mnuChoice = UI_main();
            switch (mnuChoice)
            {


                case 1:
                    Insert();
                    break;
                case 2:
                    Update();
                    break;
                case 3:
                    Delete();
                    break;
                case 4:
                    SingleRecord();
                    break;

                case 6:
                    //GetRecord();
                    break;

                default:

                    break;



            }
        }


  private static void Insert()
        {
            Console.WriteLine("Plase Enter the Following Details");
            string username;
            string password ;

            User user = new User();

            Console.WriteLine("\nEnter The Username:");
            username = Console.ReadLine();
            user.username = username;


            Console.WriteLine("\nEnter The Password:");
            password =Convert.ToString( Console.ReadLine());
            user.password = password;


               SaveUserDetails(user);
            
                Console.WriteLine("User is created Successful");
                Console.ReadLine();

            

        }



        public static void  SaveUserDetails(User ca)
        {

          
            using (ModelContainer user_entity = new ModelContainer())
            {
                user_entity.Users.Add(ca);
                user_entity.SaveChanges();

             
            }
          
        }















No comments:

Post a Comment