Wie zeigt man Daten von einem SQL Server auf einer
ASP.Net MVC Core 2 Webseite an.
1) DbContext zuweisen
Man muss das Datenbank Model erweitern mit OnModelCreating,
durch welche man die Tabellen in der SQL-Server Datenbank
Den Model-Klassen in der ASP.Net Anwendung zuordnet
Die folgende Zeile weist der Model-Klasse Hersteller die
Tabelle auf dem SQL Server zu
modelBuilder.Entity<Hersteller>().ToTable("Hersteller");
|
In der Model-Context Datei werden Tabellen und die
Zuordnung zum Server eingestellt.
/Models/app_DbContext.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ArtikelWeb.Models
{
public class app_DbContext : DbContext
{
public
app_DbContext(DbContextOptions<app_DbContext> options)
: base(options)
{ }
public DbSet<Hersteller>
Hersteller { get; set; }
public DbSet<Artikel> Artikel {
get; set; }
public DbSet<Produktgruppen>
Produktgruppen { get; set; }
protected override void OnModelCreating(ModelBuilder
modelBuilder)
{
modelBuilder.Entity<Hersteller>().ToTable("Hersteller");
modelBuilder.Entity<Artikel>().ToTable("Artikel");
modelBuilder.Entity<Produktgruppen>().ToTable("Produktgruppen");
}
}
}
|
Im Controller wird die aktuelle Datenbank angebunden als
_context.
private readonly app_DbContext _context;
|
Der Datenbank Bezug wird beim Initialisieren erstellt
//ctor
public
ArtikelController(app_DbContext context)
{
_context = context;
}
|
Und beim löschen der Klasse ebenfalls geschlossen.
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
|
ArtikelController.cs
DbContext teil
..
public class ArtikelController : Controller
{
private readonly app_DbContext _context;
//ctor
public
ArtikelController(app_DbContext context)
{
_context = context;
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
..
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ArtikelWeb.Models;
using Microsoft.EntityFrameworkCore; //*DbContext or ApplicationDbContext
namespace ArtikelWeb.Controllers
{
public class ArtikelController : Controller
{
private readonly app_DbContext _context;
//ctor
public
ArtikelController(app_DbContext context)
{
_context = context;
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
public IActionResult Index()
{
List<Artikel> artikel =
_context.Artikel.ToList();
return View(artikel);
}
public IActionResult Details(int ID)
{
Artikel artikel=
_context.Artikel.SingleOrDefault(a =>a.IDArtikel == ID);
if (artikel == null) return NotFound();
return View(artikel);
}
public IActionResult Edit(int ProductID) //*Asp.Net convention
{
//--------< Detail() >------------
return Content("ID=" + ProductID);
//--------</ Detail()
>------------
}
}
}
|
Startup
In der Datei Startup.cs wird im Bereich
ConfigureServices die Connection zum SQL Server definiert und als DbContext
angebunden
public void ConfigureServices(IServiceCollection
services)
{
var connection
= @"Server=(localdb)\mssqllocaldb;Database=ArtikelWeb;Trusted_Connection=True;";
services.AddDbContext<app_DbContext>(options =>
options.UseSqlServer(connection));
services.AddMvc();
}
|
Index
cshtml:
@model IEnumerable<ArtikelWeb.Models.Artikel
>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title )
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem =>
item.Title )
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.IDArtikel">Edit</a> |
<a asp-action="Details" asp-route-id="@item.IDArtikel">Details</a> |
<a asp-action="Delete" asp-route-id="@item.IDArtikel">Delete</a>
</td>
</tr>
}
</tbody>
</table>
|
Details.cshtml
@model ArtikelWeb.Models.Artikel
@{
ViewData["Title"] = "Detail";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Details</h2>
<div>
<h4>Produktgruppen</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title )
</dt>
<dd>
@Html.DisplayFor(model => model.Text_Content
)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.IDArtikel">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>
|
Das Microsoft Beispiel Contoso definiert die Verbindung
zum Server in der appsettings.jscon
Tutorial unter
www.youtube.com/watch?v=-jNSVzJWBS4
#ASP.Net
Core 2, MVC Model View Controller