Connect Bot Framework to DB | INSERT Data to DB using Entity Framework

Before going to Bot framework connection to DB, we will learn how to Insert data to Local SQL Database using Entity Framework. The concept will be the same for Microsoft Bot Framework.

Entity Framework Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.

Pre-requisites

  1. Visual Studio
  2. Data Storage and Processing Toolset
  3. SQL Server Management Studio (If 2nd pre-requisite not present)
  4. Connect Bot Framework to DB [Part 1] | READ data from DB using Entity Framework

Visual Studio link can be found in Downloads page.

Video

This video is in Hindi Language. You can skip and follow along with this blog.

Insert Data to DB

We will continue from where we left in the 4th prerequisite. Open the UserRespository.cs and add the below method that will insert the data to the DB.

public bool InsertEmployee(Employee employee)
        {
            bool status = false;
            try
            {
                Context.Employees.Add(employee);
                Context.SaveChanges();
                status = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return status;
        }

If insertion is successful, return true else return false. Add the following code in Program.cs file to test the above method.

Employee emp5 = new Employee();
            emp5.EmpId = "E1005";
            emp5.EmpName = "Juliet";

            bool status = userRepository.InsertEmployee(emp5);
            if (status)
            {
                Console.WriteLine("Record Inserted");
            }
            else
            {
                Console.WriteLine("Record Not Inserted");
            }

Run the console app and check the output. Also, check the data in Employee Table.

You can find the complete code of this demo in GitHub repository.

Thank you All!!! Hope you find this useful.

If you liked our content and it was helpful, you can buy us a coffee or a pizza. Thank you so much.


One thought on “Connect Bot Framework to DB | INSERT Data to DB using Entity Framework

Add yours

Up ↑

%d bloggers like this: