Wie bindet man eine bestehende Datenbank an ein
neues ASP Core MVC Projekt an?
in Visual Studio
Schritt 1: in Visual Studio den Datenbank Server
anbinden
In Menü->Views->Server Explorer anzeigen
Dann Datenverbindungen->Verbindung
hinzufügen->Servername: .\localdb
Mit Datenbank verbinden->Datenbank auswählen
Schritt 2:
im SQL Server Explorer die Datenverbindung für die Datenbank entnehmen
Unter
Datenbank->Properties->Connection->Connectionstring
Connection String in startup.cs
In Asp Core MVC die Connectionstring einfügen
In die Datei Startup.cs
->Configure Services
services.AddDbContext<ApplicationDbContext>(options
=>options.UseSqlServer("Data
Source=.\\localdb;Initial Catalog=Demo;Integrated Security=True"));
|
Tabellen
Struktur aus SQL Server entnehmen
Dann die Tabelle im SQL Server zur
Design-Ansicht öffnen und die
Tabellen-Felder mit Feldtyp anzeigen
CREATE TABLE [dbo].[tblProdDates] (
[IDProduct] INT NOT NULL,
[Date_Product] DATE NOT NULL,
CONSTRAINT
[PK_tblProdDates] PRIMARY KEY CLUSTERED ([IDProduct] ASC,
[Date_Product] ASC)
);
|
Model Klasse
in Asp Core Projekt erstellen
Die Daten Model Klasse erstellt man unter:
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using
Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MyApp.Models.DBContext;
using Microsoft.EntityFrameworkCore;
namespace MyApp
{
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)
{
// Add framework services.
services
.AddControllersWithViews()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddDbContext<ApplicationDbContext>(options
=>options.UseSqlServer("Data
Source=.\\localdb;Initial Catalog=Demo;Integrated Security=True"));
}
// This
method gets called by the runtime. Use this method to configure the HTTP
request pipeline.
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Demo}/{action=Index}");
});
}
}
}
|
Model Class
Data-Definition of Table
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace MyApp.Models
{
public class ProductDateModel
{
[Key]
public long IDProduct { get; set; }
public DateTime Date_Product { get; set; }
}
}
|
Erweiterung der Model Klasse mit PrimaryKey
Cs0246 The
type or namespace name "KeyAttribute" could not be found (are you
missing directive or an assembly reference)?
using
System.ComponentModel.DataAnnotations;
|
AppplicationDbContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//< Using>
using Microsoft.EntityFrameworkCore;
//</ Using>
namespace MyApp.Models.DBContext
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options): base(options){ }
#region EF: Datase-Tabels to Models
//------------<
region: Datase-Tables to Models >------------
public
DbSet<Models.ProductDateModel> tbl_ProductDates{ get; set; }
protected override void OnModelCreating(ModelBuilder
modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Models.ProductDateModel>().ToTable("tbl_ProductDates");
}
//------------</
region : Datase-Tables to Models >------------
#endregion /EF: Datase-Tabels to Models
}
}
|
Für EF
EntityFramework, SQL Server Visual Studio