DEV Community

Manik
Manik

Posted on • Updated on

How to Web API .Net Core Basics to Advanced Part2 DB

Configuring Data
Welcome back!

Every application in the modern world needs to work with data. Why not? We first deal with big fat DATA problems. To do this we can start with code first approach by creating Entities or Models, so let us create a folder in our project called Entities and after that we can add a class file and name it Company.cs. I am hoping you have some experience with entities.
Asp.Net 5 Web API

Code First
In code first we should think about all the cloumns we want in our tables. We will need to declare them in entities class. I have given very basic examples of DataAnnotations and you should do your own research and use them accordingly.
Asp.Net 5 Web API Database

Adding our model to database
In order to add our models to database we need to first set up a few things such as DB connection string, installing few nuget packages and configuring our Datacontext.

To start with we are going to add Database connection string to appsettings.json file.
Asp.Net 5 Web API Database

Once you are happy with your appsettings.json changes we want to create our database and company table. To do this we need to set up our Database context object. Go ahead and add another folder to your project which I am going to call Data, you can call it what ever you feel comfortable with. Inside Data folder we are going to add class file and I am going to name it DataContext.cs, some people like to name it AppDataContext so it is up to you what you want to name it.
Asp.Net 5 Web API Database

Now before we configure our Database Context we need Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Tools and Microsoft.EntityFrameworkCore.SqlServer nuget packages. You can add them by using nuget package manager or simply adding it in your .csproj file. Either way, you will see it appear in your .csproj file.
Asp.Net 5 Web API Database

After installing Microsoft.EntityFrameworkCore we can get on with setting up our DataBase context object. Notice that our DataContext class is inheriting from DbContext which is part of Microsoft.EntityFrameworkCore all the heavy lifting is done by Entity Framework we just need to provide it with enough information.

public class DataContext:DbContext
{

public DataContext(DbContextOptions<DataContext> options)
 : base(options)  { }

    public DbSet <Entities.Company>  Companies { get; set; }

 }

Enter fullscreen mode Exit fullscreen mode

Now we need to make sure we add our DataContext.cs file in startup.cs file. DotNet core comes with inbuilt dependency injection which makes it a lot easier for us to impliment IoC by using inbuilt IoC container, if you want to know more about Dependency injection in ASP.NET Core. Next, we are going to set up DataContext object so it can be injected when it is needed. To do that first, we need Microsoft.EntityFrameworkCore.SqlServer. We added SqlServer package in the last step so we can get on with configuring startup.cs file.

public void ConfigureServices(IServiceCollection services)
{

services.AddDbContext<DataContext>
(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

   services.AddControllers();
   services.AddSwaggerGen(c =>
    {
     c.SwaggerDoc("v1", new OpenApiInfo { Title = "AspNetWebApiSeriesCore5", Version = "v1" });
     });
}  

Enter fullscreen mode Exit fullscreen mode

Look at that! we are now all set to push it to our Database :) this is easy as most of the work is done by EF core. Go to tools in your Visual Studio IDE and select.
Asp.Net 5 Web API Database

To set up our first migration, we can run add-migration AddCompanyToDB once build succeeded, we can run update-database command and that is it. Easy peasy! ;)

Asp.Net 5 Web API Database

Part 3 How to Web API Core Data Access Layer >>>

<<<Part 1 How to set up Web API core project

Discussion (0)