Let's discuss how to connect to databases.
We have already implemented basic CRUD operations with mockup data. Now, we are going to extend our application from last discussion Crud Operations in Asp.net Core 1.0 Mvc Application - Part Six to connect with SQL Server Database. And, we will be using DataAccess Project from .NET Core 1.0 Connecting SQL Server Database.
A .NET Core application can connect to a database through Database Provider. We are going to use an SQL Server Database ContactDB, therefore, we are going to use Microsoft SQL Server Database Provider.
Create Data Access Project
- Open existing Solution in Visual Studio 2015.
- Add new WebApplicationCore.NetCore.DataAccess and add reference to Microsoft.EntityFrameworkCore.SqlServer and BaseDataAccess class following steps in .NET Core 1.0 Connecting SQL Server Database.
- We are going to use IConfigurationRoot to get web application configurations. So, add Reference to Microsoft.Extensions.Configuration.Abstractions as following.
- Open NuGet Manager through WebApplicationCore.NetCore.DataAccess Reference ContextMenu >> References >> Manage NuGet Packages. In Browse tab, search for "Microsoft.Extensions.Configuration.Abstractions" and install.
- Add new Constructor with parameter of IConfigurationRoot type to get configurations.
- public BaseDataAccess(IConfigurationRoot configuration) {
- this.ConnectionString = configuration["ConnectionStrings:DefaultConnection"];
- }

Add ContactDataAccess to WebApplicationCore.NetCore.DataAccess
- Add reference to “WebApplicationCore.NetCore.Model” because we are going to use Contact Model Class in project.
- Open WebApplicationCore.NetCore. BusinessLogic References >> Add References >> Reference Manager Screen >> Projects >> Solution >> Select WebApplicationCore.NetCore.Model.
- Click OK.
- Add new Interface.
- Open Add New Item Screen through Solution Context Menu >> Add >> New Item >> Installed >> .NET Core >> Code >> Interface.
- Name it IContactDataAccess.cs.
- Click OK Button.
- It will add a new interface in project.
- Define CRUD operations in interface.
- Add new Class.
- Open Add New Item Screen through Solution Context Menu >> Add >> Class >> Installed >> .NET Core >> Code >> Class.
- Name it ContactDataAccess.cs.
- Click OK Button.
- It will add a new class in project.
- Make ContactDataAccess child of BaseDataAccess and implement IContactDataAccess.
- Implement code to perform CRUD operation from database.
- public interface IContactDataAccess {
- Contact GetContact(int contactId);
- List < Contact > GetContacts();
- Contact CreateContact(Contact contact);
- bool UpdateContact(Contact contact);
- bool DeleteContact(int contactId);
- }
- public class ContactDataAccess: BaseDataAccess, IContactDataAccess {
- public ContactDataAccess(IConfigurationRoot configuration): base(configuration) {}
- public Contact GetContact(int contactId) {
- Contact contactItem = null;
- List < DbParameter > parameterList = new List < DbParameter > ();
- parameterList.Add(base.GetParameter("ContactId", contactId));
- using(DbDataReader dataReader = base.ExecuteReader("Contact_Get", parameterList, CommandType.StoredProcedure)) {
- if (dataReader != null && dataReader.HasRows) {
- if (dataReader.Read()) {
- contactItem = new Contact();
- contactItem.ContactId = (int) dataReader["ContactId"];
- contactItem.Name = (string) dataReader["Name"];
- contactItem.Address1 = (string) dataReader["Address1"];
- contactItem.Address2 = (string) dataReader["Address2"];
- contactItem.City = (string) dataReader["City"];
- contactItem.ProvinceState = (string) dataReader["ProvinceState"];
- contactItem.ZipPostalCode = (string) dataReader["ZipPostalCode"];
- contactItem.Country = (string) dataReader["Country"];
- contactItem.ContactNumber = (string) dataReader["ContactNumber"];
- contactItem.Email = (string) dataReader["Email"];
- contactItem.WebSite = (string) dataReader["WebSite"];
- }
- }
- }
- return contactItem;
- }
- public List < Contact > GetContacts() {
- List < Contact > contacts = new List < Contact > ();
- Contact contactItem = null;
- List < DbParameter > parameterList = new List < DbParameter > ();
- using(DbDataReader dataReader = base.ExecuteReader("Contact_GetAll", parameterList, CommandType.StoredProcedure)) {
- if (dataReader != null) {
- while (dataReader.Read()) {
- contactItem = new Contact();
- contactItem.ContactId = (int) dataReader["ContactId"];
- contactItem.Name = (string) dataReader["Name"];
- contactItem.Email = (string) dataReader["Email"];
- contactItem.WebSite = (string) dataReader["WebSite"];
- contacts.Add(contactItem);
- }
- }
- }
- return contacts;
- }
- public Contact CreateContact(Contact contact) {
- List < DbParameter > parameterList = new List < DbParameter > ();
- DbParameter contactIdParamter = base.GetParameterOut("ContactId", SqlDbType.Int, contact.ContactId);
- parameterList.Add(contactIdParamter);
- parameterList.Add(base.GetParameter("Name", contact.Name));
- parameterList.Add(base.GetParameter("Address1", contact.Address1));
- parameterList.Add(base.GetParameter("Address2", contact.Address2));
- parameterList.Add(base.GetParameter("City", contact.City));
- parameterList.Add(base.GetParameter("ProvinceState", contact.ProvinceState));
- parameterList.Add(base.GetParameter("ZipPostalCode", contact.ZipPostalCode));
- parameterList.Add(base.GetParameter("Country", contact.Country));
- parameterList.Add(base.GetParameter("ContactNumber", contact.ContactNumber));
- parameterList.Add(base.GetParameter("Email", contact.Email));
- parameterList.Add(base.GetParameter("WebSite", contact.WebSite));
- base.ExecuteNonQuery("Contact_Create", parameterList, CommandType.StoredProcedure);
- contact.ContactId = (int) contactIdParamter.Value;
- return contact;
- }
- public bool UpdateContact(Contact contact) {
- bool updated = true;
- List < DbParameter > parameterList = new List < DbParameter > ();
- parameterList.Add(base.GetParameter("ContactId", contact.ContactId));
- parameterList.Add(base.GetParameter("Name", contact.Name));
- parameterList.Add(base.GetParameter("Address1", contact.Address1));
- parameterList.Add(base.GetParameter("Address2", contact.Address2));
- parameterList.Add(base.GetParameter("City", contact.City));
- parameterList.Add(base.GetParameter("ProvinceState", contact.ProvinceState));
- parameterList.Add(base.GetParameter("ZipPostalCode", contact.ZipPostalCode));
- parameterList.Add(base.GetParameter("Country", contact.Country));
- parameterList.Add(base.GetParameter("ContactNumber", contact.ContactNumber));
- parameterList.Add(base.GetParameter("Email", contact.Email));
- parameterList.Add(base.GetParameter("WebSite", contact.WebSite));
- int returnValue = base.ExecuteNonQuery("Contact_Update", parameterList, CommandType.StoredProcedure);
- updated = returnValue > 0;
- return updated;
- }
- public bool DeleteContact(int contactId) {
- bool deleted = false;
- List < DbParameter > parameterList = new List < DbParameter > ();
- parameterList.Add(base.GetParameter("ContactId", contactId));
- int returnValue = base.ExecuteNonQuery("Contact_Delete", parameterList, CommandType.StoredProcedure);
- deleted = returnValue > 0;
- return deleted;
- }
- }

Change WebApplicationCore.NetCore.BusinessLogic
- Add reference to “WebApplicationCore.NetCore.DataAccess” in WebApplicationCore.NetCore.BusinessLogic because we are going to use ContactDataAccess in this project.
- Open WebApplicationCore.NetCore. BusinessLogic References >> Add References >> Reference Manager Screen >> Projects >> Solution >> Select WebApplicationCore.NetCore.DataAccess.
- Click OK Button.
- Although we are not going to use Configuration in BusinessLogic, yet we may need it in future. To use IConfigurationRoot to get web application configurations, add Reference to Microsoft.Extensions.Configuration.Abstractions as following,
- Open NuGet Manager through WebApplicationCore.NetCore.BusinessLogic Reference context menu >> References >> Manage NuGet Packages. in Browse tab search for "Microsoft.Extensions.Configuration.Abstractions" and install.
- Add new Interface.
- Open Add New Item Screen through Solution Context Menu >> Add >> New Item >> Installed >> .NET Core >> Code >> Interface.
- Name it IContactBusinessLogic.cs.
- Click OK Button.




Manav PandyaPosted Sep 24, 2016, 4:21 AM
Thnaks sir ...