Example Project in ASP.NET CORE
- A simple Web Form
- Let's write the code
General pattern for routing in MVC
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Adding Database Connection in .NET CORE
- Connection string in appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=[server-name];Database=Bulky;Trusted_Connection=True;TrustServerCertificate=True;"
}
}
using ExampleProject.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
namespace ExampleProject.Data
{
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
}
}
- Final settings is add it to the program.cs file
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
Tools That needs to be instaleld
- EntityFrameworkCore
- EntityFrameworkCore.SqlServer
- EntityFrameworkCore.Tools
Commands for migrating the database
make-migration [migration_name]
update-database
Validating the form on client side
- Add Following line of code at the end of the view
<!-- Add Partial View -->
@section Scripts{
@{
<partial name="_ValidationScriptsPartial" />
}
}