Base article
This article is going to handle the connection of an existing website to a local database using the entity framework Core. So, the starting point is going to be the following article:
What is Entity Framework Core?
EF Core is a lightweight, extensible, and cross-platform version of Entity Framework Read more about Entity Framework Core.
Benefits of using Entity Framework Core
- Faster database setup
- Easier to access data, reducing time to read and access data
- Work directly with your complex objects, not with datasets/data tables
- Less code, more control
- No need to write queries, we use LINQ for Entities
Entity Framework vs Entity Framework Core
Entity Framework
- Runs only on Windows
- Can be used with .Net Framework
- No more new features will be added here
Entity Framework Core
- Is Cross-platform, so is not executable only on Windows
- Can be used with .Net Core or .Net Framework
- Keeps growing and getting better every day
Here we have a full comparison of functionalities between both.
Related articles
- ASP.NET Core Sample Website - Calculator
- .Net Core Application With Azure Hosted Database
- Using .NET Core Data Annotation
- Azure Hosted .Net Core Application
- Setting Up Entity Framework Core Database With Factory
- .NET Core REST API With Swagger
CRUD Implementation Step-by-Step
Adjust the model in order to make it able to be stored in a database,
public class ValueSamples
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
Create your DB context file
This is the class that is going to connect to your database, each object declared inside the DbSet type is going to be a table.
public class CrudSampleContext : DbContext
{
public CrudSampleContext(DbContextOptions<CrudSampleContext> options) : base(options)
{
}
public DbSet<ValueSamples> ValueSamples { get; set; }
}
Adjust your StartUp class
You must register and initialize your DB context using dependency injection.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<CrudSampleContext>(options => options.UseSqlServer("your database connection string"));
// Register Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Sample API", Version = "version 1" });
});
}
Getting your database connection string
- Open Server Explorer and click on "Connect to Database".

- Select Microsoft SQL Server.

- Pick up your server name and push OK.

- Check the result.

- Right-click on your newly added Data Connection and go to properties.

- Get your connection string.

- Your final Start Up class
With your connection string, your StartUp should look like this.
The items in yellow were added from the connection string above.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<CrudSampleContext>(options => options.UseSqlServer(@"Data Source=yourServer;Integrated Security=True;Database=CRUDSample"));
// Register Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Sample API", Version = "version 1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}
app.UseMvc();
}
}














Bidyasagar MishraPosted Jul 27, 2019, 11:42 AM
Nice article