- ASP.NET Core Web Application
- Web Application (Model-View-Controller)
- create model
using System;
using System.ComponentModel.DataAnnotations;
namespace WebApplication.Models
{
public class Movie
{
public int Id { get; set; }
[StringLength(60, MinimumLength = 3)]
[Required]
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")]
[Required]
[StringLength(30)]
public string Genre { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
[StringLength(5)]
[Required]
public string Rating { get; set; }
}
}
- Add NuGet Packages
Install-Package Microsoft.EntityFrameworkCore.SqlServer
- Create a database context class
using Microsoft.EntityFrameworkCore;
using WebApplication.Models;
namespace WebApplication.Data
{
public class MvcContext : DbContext
{
public MvcContext (DbContextOptions<MvcContext> options)
: base(options)
{
}
public DbSet<Movie> Movie { get; set; }
}
}
- Register the database context at
Startup.cs
using WebApplication.Data;
using Microsoft.EntityFrameworkCore;
Startup.ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MvcContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MvcContext")));
}
- Add a database connection string
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MvcContext": "Server=(localdb)\\mssqllocaldb;Database=MvcContext-1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
- Scaffold
- Right Click on Controller Directory > Add > New Scaffold Item
- Select MVC Controller with views, using Entity Framework
- Migration
Add-Migration InitialCreate
Update-Database