AspNet Core Web API Part2 DB
Tags: csharp, database, dotnet, webdev
Published: 2021-07-08 15:16:06

**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.
![AspNet Core Web API ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9ymjcb548w74hq9t5ii.JPG)

**Code First**
In code first we should think about all the columns 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.
![AspNet Core Web API ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8axl1gcd17mb6ic73uh.JPG)

**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.
![AspNet Core Web API ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htiviyfdgmpufhw1sly2.JPG)

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. 
![AspNet Core Web API Database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16nw0nyk7tr7gu6tx0bi.JPG)

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.
![AspNet Core Web API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53f9xuz0fcm6sdf8n1yz.JPG)


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.

```C#
public class DataContext:DbContext
{

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

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

 }

```

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](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0). 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.
```C#
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" });
     });
}  

```

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.
![AspNet Core Web API ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0uwpzh9363ctzpwwhnl.jpg)

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! ;)

![AspNet Core Web API ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vp291hiz3grdr3ft1vmc.JPG)

  


[AspNet Core Web API Part 3 Data Access Layer >](https://dev.to/scorpio69/how-to-web-api-net-core-basics-to-advanced-part3-data-access-layer-1k73)





[Part 1 AspNet Core Web API  project](https://dev.to/scorpio69/how-to-web-api-net-core-basics-to-advanced-part1-486b)_