NetAcademia "Einblick in Objekt -orientierte Programmierung im Web: The Restaurant Project" -Kurscode
Felhasználók Webalkalmazás
+----------------------------+ +-------------------------------------------+
| | | |
| +----------------------+ | | Publikus oldalak |
| | Ismeretlen | | | +-----------------------+ |
| | felhasználó | | +-----------------------> | | | |
| | (nem azonosítottuk) | | | | Étlap megtekintése | |
| | | | | | | |
| | | | ^--------------> | | | |
| | | | | | | | |
| +----------------------+ | | | +-----------------------+ |
| | | | |
| | | | |
| | | | |
| | | | Nem publikus oldalak |
| +---------------+------+ | | | +-----------------------+ |
| | Azonosított | | | | | | | |
| | felhasználó |jogok | | | | | Étlap módosítása | |
| | (átment az | | | +--------> ha van joga | | | |
| | azonosításon) | | | | | | | |
| | | | | +--------------> | | | |
| +---------------+------+ | | +-----------------------+ |
| | | |
+----------------------------+ | |
| |
+-------------------------------------------+
Dies ist überhaupt keine triviale Aufgabe: Identifikation und Zulassungsmanagement
ASP.NET Identity löst es für uns.
C# Projekt, ASP.NET -Webanwendung (.NET Framework):

Adatbázis Alkalmazás
+------------------------+ +---------------------------+
| | | |
| Text állomány | +-------> | ami az adatokat |
| - csv | | használja |
| | xml | <-------+ | |
| - json | | |
| Komolyabb adatbázis | | |
| -sqlight | | |
| -sql szerver | | |
| | | |
| | | |
| | | |
| | | |
+------------------------+ +---------------------------+
Telepítéskor: egyszerre kell kialakítani az adatbázist és az alkalmazást
a célgépen
Identität zeigt eine sehr verlockende Lösung: Ich beginne den Alkohol und Sie benötigen keine Installation, er erstellt seine eigene Datenbank im Hintergrund.
Irgendwie wird die Datenbank nicht automatisch erstellt, sondern sich darauf auswirken.
Entity Framework
Adatbázis Code First Alkalmazás
+------------------------+Migrations +---------------------------+
| | | |
| -MS SQL Szerver | + | ami az adatokat |
| | | | használja |
| | v | |
| | | |
| | <--------+ | Adatmodell módosítás |
| | | |
| | <--------+ | Adatmodell módosítás |
| | | |
| | <--------+ | Adatmodell módosítás |
| | | |
| | <--------+ | Adatmodell módosítás |
+------------------------+ +---------------------------+
Telepítéskor: Az alkalmazás hozza létre az adatbázist magának
Voraussetzung: Die Existenz des EntityFramework -Nuget -Packs
Lizenzierung
PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project OopRestaurant201807.
Dadurch wurde die Station Migrations configuration.cs erstellt.
Ein Identitätsmodell in einen Migrationsschritt fordern
PM> add-migration 'Identity datamodel'
Scaffolding migration 'Identity datamodel'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Identity datamodel' again.
Der Schritt ist willkürlich, ich habe "Identity Datamodel" bezeichnet, damit ich diesen Schritt später identifizieren konnte.
Dadurch wurden die Migrationen 201807050914249_Identity Datamodel.cs -Datei (und zwei weitere technische Dateien) erstellt.
Dies heißt: Ein Änderungsschritt, Migrationsschritt. Es gibt zwei wichtige Teile: Up () und Down () -Funktionen.
Migrationsschritt in die Datenbank einsetzen
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Es wird nicht zuerst ausgeführt, der Grund dafür ist, dass Web.config den Datenzugriffsweg hat, den Sie suchen. Ich habe diese MDF -Datei bereits gelöscht, also gibt es so etwas, also geht es verloren. Wenn Sie die Datenverbindungseinstellung von web.config löschen, wird sie ohne Probleme ausgeführt.
Erstellt diese Datenbank:

Die Datenbank auf einer eigenen Maschine ist in der Standard -SQL -Instanz definiert und als StandardConnection bezeichnet.
Wir erstellen eine Verbindungseinstellung mit https://www.connectionstings.com/.
< connectionStrings >
< add name = " DefaultConnection " connectionString = " Server=.SQLEXPRESS;Database=OopRestaurantDb;Trusted_Connection=True "
providerName = " System.Data.SqlClient " />
</ connectionStrings >Es ist wichtig, den Servernamen, den Datenbanknamen und die Methode einzugeben, wenn sich der Benutzer anmeldet.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Running Seed method.
Wenn Sie erneut Update-Daten ausführen, schreiben Sie Folgendes:
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
Unser Gerät kennt also die Modellversion, die Datenbankversion und weiß, dass nichts fehlt.
Sie wissen das mit dem __migrationHistory -Tisch.
Die Dateinamen im Migrationsverzeichnis enthalten die im Code enthaltenen Datenbankänderungsschritte. Die __MigrationHistory -Tabelle enthält die Schritte in der Datenbank.
Alle Auszahlungen (auf Version 0 zurücksetzen)
PM> update-database -t 0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Reverting migrations: [201807050914249_Identity datamodel].
Reverting explicit migration: 201807050914249_Identity datamodel.
PM>
A -T ist eine Abkürzung für den Parameter von TargetMigration und 0 ist der Zustand vor jedem Schritt
PM> update-database -Script
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Der Parameter des -Skripts wird nicht heruntergefallen, zeigt uns jedoch das durch Migrationsschritt generierte SQL -Skript an.
A kódban lévő módosító lépések Az adatbázisban lévő módosító lépések
+-------------------------------------+ +---------------------------------+
| | | |
| | A hiányzó lépések | |
| A Migrations mappa alatt lévő | kerülnek az adatbázisba | |
| egyes lépések állományai | | A __MigrationHistory táblában |
| | | lévő sorok |
| | update-database | |
| | | |
| | +---------------------> | |
| | | |
| | Ez a migration step-ben | |
| | lévő köztes nyelvből | |
| | az adatbázisnak megfelelő| |
| | SQL scriptet gyárt, majd | |
| | lefuttatja az SQL | |
| | szerveren | |
| | | |
| | | |
| | | |
| | | |
| ^ | | |
| | | | |
+-------------------------------------+ +---------------------------------+
|
|
+
A modell módosítása után,
az add-migration paranccal készülnek
a módosító lépések
Wir schauten uns das Gundel Restaurant -Menü an und kamen Folgendes:
Durch das Erstellen unseres eigenen Modells können wir es mit der ApplicatondbContext -Klasse in models identityModels.cs an die Identitätsdatenbank verbinden.
Wenn ich mit einer Datenbank arbeite, die Schritte verfehlt, kann ich keinen weiteren Migrationsschritt hinzufügen:
PM> add-migration 'add MenuItem table'
Unable to generate an explicit migration because the following explicit migrations are pending: [201807050914249_Identity datamodel]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
Bevor Sie eine Update-Daten benötigen:
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.
Der Hinweis ist, dass Sie die Datenbank nicht mit unserem Modell in Einklang bringen können, da es eine Modelländerung gibt, die sich noch nicht in einem Änderungsskript befindet. Dies ist jedoch nur eine gelbe Warnung, in der Migrationsschritte in der Datenbank gespielt werden, damit wir die nächste Datenbankänderung aus der Modelländerung erstellen können.
PM> add-migration 'add MenuItem table'
Scaffolding migration 'add MenuItem table'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration add MenuItem table' again.
Anschließend können Sie die Datenbank mit Aktualisierungsdatenbank aktualisieren.
Wir können die vollständige Reihe von Schritten zurückziehen oder gleichzeitig ausführen:
PM> update-database -t 0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Reverting migrations: [201807051033248_add MenuItem table, 201807050914249_Identity datamodel].
Reverting explicit migration: 201807051033248_add MenuItem table.
Reverting explicit migration: 201807050914249_Identity datamodel.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807050914249_Identity datamodel, 201807051033248_add MenuItem table].
Applying explicit migration: 201807050914249_Identity datamodel.
Applying explicit migration: 201807051033248_add MenuItem table.
Running Seed method.
Wir können jeweils und zurück mit diesen Schritten laufen:
Wenn ich den Namen des Ziels in einer leeren Datenbank gebe, werden die Änderungsschritte nur bis dahin ausgeführt
PM> update-database -t '201807050914249_Identity datamodel'
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Natürlich kann ich die fehlenden ohne Parameter ausführen
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201807051033248_add MenuItem table].
Applying explicit migration: 201807051033248_add MenuItem table.
Running Seed method.
Und ich kann mich zu einer bestimmten Version zurückziehen:
PM> update-database -t '201807050914249_Identity datamodel'
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Reverting migrations: [201807051033248_add MenuItem table].
Reverting explicit migration: 201807051033248_add MenuItem table.
Wiederholen
Felhasználó böngésző
SQL ADATBÁZIS ASP.NET MVC szerveralkalmazás (HTML/CSS/JavaScript)
+----------------------------+ +------------------------+ +------------------------+
| | | | | |
| | +--------------> | | +----------------> | |
| | | | | |
| | | | | |
| | <--------------+ | | <----------------+ | |
| | | | | |
| | | | | |
| | | +-------+ +------+ | | |
| | | | | | | | | |
| | | | | <+ | | | | |
| | <--------------+ | | | | | | | |
| | | | | | | | | |
| | | +-------+ +------+ | | |
+----------------------------+ +------------------------+ +------------------------+
^
|
Migrációs +
lépések Adatmodell
Lesen: Das Gesetz der Verlaufabstraktionen
Wir erstellen eine Kategorie für jedes Lebensmittel. Aber wir setzen es nicht in den Menuitem -Tisch (dh die Menuitem -Linie), sondern betonen es in unserem eigenen Tanz, was es für unsere eigene Klasse bedeutet.
| Zeit | Name | Beschreibung | Preis | Kategorie |
|---|---|---|---|---|
| 1 | Sea Fish Trio | Atlantischer Lachs Tatar mit mariniertem Lachsfilet und Thunfisch mit Salzwasser | 7500 | 1 |
| 3 | Kalb | Knödel | 4500 | 1 |
| Zeit | Kategorie |
|---|---|
| 1 | Suppen, Vorspeisen |
Wir erstellen die Kategorieklasse und einen Link aus der Menuitem -Klasse.
Dann folgen diese beiden Schritte:
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
Um das Menü anzuzeigen
LINQ -Informationen mit dieser Google -Suche: Linq 101
| Kategorie.name | Name | Beschreibung | Preis |
|---|---|---|---|
| Vorspeisen | Sea Fish Trio | Atlantischer Lachs Tatar mit mariniertem Lachsfilet und Thunfisch mit Salzwasser | 7500 |
| Vorspeisen | Kalb | Knödel | 4500 |
Gelegenheiten:
Kategorien
+-------------------------------------------------+ +---------------------------------------+
| | A választott+-----> | |
| | érték +---------------------------------------+
| | | |
| | | +--------------------------------+ |
| +----------------------+ | | |
| Name: | | | | +--------------------------------+ |
| +----------------------+ | A lehetséges | |
| +----------------------+ | értékek +-----> | +--------------------------------+ |
| Description: | | | felsorolása | |
| +----------------------+ | | +--------------------------------+ |
| +----------------------+ | | |
| Price: | | | | +--------------------------------+ |
| +----------------------+ | | |
| | | |
| | | |
| +----------------------+---+ | | |
| Category: | | | | | |
| +----------------------+---+ | | |
| | | |
| | | |
| | | |
| | | |
| +---------+ +---------+ | | |
| | Save | | Cancel | | | |
| +---------+ +---------+ | | |
| | +---------------------------------------+
| |
| |
| |
+-------------------------------------------------+
Datenanforderung für die Anzeige der Trennung:
<-------------------------------------------------^
| |
Adatbázis | EntityFramework | Adatmodell
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
| | | | +----------------------------------+ | | Attach |
| | | | | DbContext.MenuItems | | <--------------------------^ |
| | | | +----------------------------------+ | | MenuItem | |
| | | | MenuItemEntry | | +------------------+-+ |
| | | | +----------------+ Entry | | | | |
| v | | | | <------------------------+ | |
| | | | | | | | | |
| Categories MenuItems | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | |
| | | | | | | | | | | | Category | |
| | | | | | | | | | | | +---------------+ | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | Category | <-----+ | MenuItem | | | | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | | | | | | +-+--------------+ | | | +---------------+ | |
| | | | | | | | | | | | |
| +-------------+ +-------------+ | | | Reference | | | | |
| | | | Load | | | | |
| | | | +-----------+ | | +--------------------+ |
| <----SaveChanges-------------+ +--------> | Category | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
Die Datenbank- und EntityFramework -Konversationen können in den SQL Server -Profiler aufgenommen werden.
Start SQL Server Profiler: SQL Server Management Studio Tools SQL Server Profiler
Das ASP.NET -Identitätsdatenmanagement besteht aus mehreren Ebenen:
+-------------------------------------------------------------+
| |
| |
| |
| |
| +-------------+ +--------------+ +-----------------+ |
| | | | | | | |
| | Adatbázis | | UserStore | | UserManager | <------+
| | | | | | | |
| | | | | | | |
| | | | <-------------+ | |
| | | | | | | |
| | | | | | | |
| | <----------------+ | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| +-------------+ +--------------+ +-----------------+ |
| |
| |
| |
| |
| |
+-------------------------------------------------------------+
< select data-val =" true "
data-val-number =" The field CategoryId must be a number. "
data-val-required =" The CategoryId field is required. "
htmlattributes =" { class = form-control } "
id =" CategoryId " name =" CategoryId " >
< option value ="" > - Válassz egy lehetőséget - </ option >
< option value =" 1 " > Hideg előételek </ option >
< option value =" 2 " > Levesek </ option >
< option value =" 3 " > Meleg előételek </ option >
</ select >Der Grund für den Fehler ist daher, dass dies ist
htmlattributes="{ class = form-control }" class="form-control"Die Lösung besteht darin, den Parameter aus dem vorherigen Term Bearbeitungseingangsgenerierung zu kopieren, in dem er als zusätzlich -viewData -Parameter angegeben wurde. @Html.dropdownListfor () erwartet jedoch einen Parameter htmlattributes , der sich in einem anderen Format befindet.
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes) at system.web.mvc.html.Selectextesions.dropdownlistfor [tmodel, tProperty] (htmlHelper`1 htmlHell, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, expression, ienalewer System.web.mvc.html.selectextensions.dropdownlistfor [tmodel, tpropeerty] (htmlhelper`1 htmlhelper, expression`1 expression, ienumerable`1 Selectlist, Object htmlatributes) at ASP._PAGE_VIEWS_SHARED_EDITORTEMPLTES_TABLE_CSHTML.EXECUTE () In d: repos ooprestaurant201807 ooprestaurant201807 Views Shared editorteMplates table.cshtml: Zeile 19 (...) akkor a hiba oka az, hogy a lenyíló adattartalmát (jelen esetben AssignablesLocations nem inicializáltuk)
Unterschiedliche mögliche Validierungsstellen: Gültig die Anwendung jedes Mal und schützen Sie die Datenbank, sofern möglich!
Felület Alkalmazás Adatbázis
+----------------------+ +-------------------+ +----------------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | +---------------------> | | +--------------------> | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+----------------------+ +-------------------+ +----------------------+
^ ^ ^
| | |
| | |
+ + +
Validálás Validálás Validálás
+----------------------------------------------------------------------------------------------------------------------+
| Mindenki |
| |
| |
| +-------------------------------------------------------------------------+ |
| | Bejelentkezett felhasználók | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | +----------------------------+ +------------------+ +------------+ | |
| | | Admin | | Pincér | | Szakács | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | Ő mindent tud | | Asztalokat | | Menüt | | |
| | | | | mozgathat | | írhat | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | +----------------------------+ +------------------+ +------------+ | |
| | | |
| +-------------------------------------------------------------------------+ |
| |
+----------------------------------------------------------------------------------------------------------------------+
Wenn Sie nach dem Herunterladen des Code einen solchen Fehler treffen:
PM> update-database
& : File C:UsersadminReposOopRestaurant201807packagesEntityFramework.6.2.0toolsinit.ps1 cannot be loaded because running scripts is disabled on this system. Fo
r more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:45
+ ... rgs+=$_}; & 'C:UsersadminReposOopRestaurant201807packagesEntity ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
update-database : The term 'update-database' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ update-database
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (update-database:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Dies kann eine Lösung sein:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Visual Studio Exit and Entry, dann wieder aufbauen und aktualisieren.