Netacademia "Aperçu de la programmation orientée vers l'objet sur le code de cours Web: The Restaurant Project"
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) | | | | | | | |
| | | | | +--------------> | | | |
| +---------------+------+ | | +-----------------------+ |
| | | |
+----------------------------+ | |
| |
+-------------------------------------------+
Ce n'est pas du tout une tâche triviale: l'identification et la gestion de l'admissibilité
L'identité ASP.NET le résout pour nous.
C # Project, application Web ASP.NET (.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
L'identité montre une solution très tentante: je commence l'alcool et vous n'avez besoin d'aucune installation, il crée sa propre base de données en arrière-plan.
D'une manière ou d'une autre, elle ne crée pas automatiquement la base de données, mais pour avoir un effet à ce sujet.
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
Préalable: l'existence du pack Nuget entité
licence
PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project OopRestaurant201807.
Cela a créé la station Migrations Configuration.cs .
Pour appeler à un modèle d'identité dans une étape de migration
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.
L'étape est arbitraire, j'ai appelé «identité dataModel» pour que je puisse identifier cette étape plus tard.
Cela a créé les migrations 201807050914249_Identity Datamodel.cs Fichier (et deux autres fichiers techniques)
C'est ce qu'on appelle: une étape de modification, étape de migration. Il y a deux parties importantes: les fonctions UP () et Down () .
Étape de la migration dans la base de données
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.
Il ne s'exécute pas en premier, la raison en est que web.config dispose du chemin d'accès aux données que vous recherchez. J'ai déjà supprimé ce fichier MDF, donc il n'y a rien de tel, donc il se perd. Si vous supprimez le paramètre de connexion de données de web.config , il s'exécutera sans aucun problème.
Crée cette base de données:

La base de données sur sa propre machine est définie sur l' instance SQL par défaut et est appelée DefaultConnection .
Nous faisons un paramètre de connexion avec https://www.connectionstings.com/.
< connectionStrings >
< add name = " DefaultConnection " connectionString = " Server=.SQLEXPRESS;Database=OopRestaurantDb;Trusted_Connection=True "
providerName = " System.Data.SqlClient " />
</ connectionStrings >Il est important de saisir le nom du serveur, le nom de la base de données et la méthode en tant qu'utilisateur se connecte.
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.
Si vous exécutez à nouveau la database de mise à jour, vous écrivez ceci:
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.
Ainsi, notre appareil connaît la version modèle, la version de la base de données, et sait que rien ne manque.
Vous le savez avec la table __migrationhistory.
Les noms de fichiers du répertoire des migrations contiennent les étapes de modification de la base de données qui se trouvent dans le code. Le tableau __MigrationHistory contient les étapes de la base de données.
Tout le retrait (réinitialiser à la version 0)
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 est une abréviation du paramètre TargetMigration et 0 est l'état avant chaque étape
PM> update-database -Script
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Le paramètre -Script ne fait pas fin, mais il nous montre le script SQL généré par étape de migration.
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
Nous avons examiné le menu du restaurant Gundel et sommes arrivés à ce qui suit:
En créant notre propre modèle, nous pouvons le connecter à la base de données d'identité à l'aide de la classe applicatondBContext dans Modèles IdentityModels.cs.
Si je travaille avec une base de données qui manque des étapes, je ne peux pas ajouter une autre étape de migration:
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.
Avant d'avoir besoin d'une database de mise à jour :
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.
La note est que vous ne pouvez pas apporter la base de données en ligne avec notre modèle car il y a un changement de modèle qui n'est pas encore dans un script de changement. Cependant, ce n'est qu'un avertissement jaune, en jouant des étapes de migration dans la base de données afin que nous puissions créer le prochain changement de base de données à partir du changement de modèle.
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.
Ensuite, vous pouvez mettre à jour la base de données avec la base de données de mise à jour.
Nous pouvons retirer la série complète d'étapes ou l'exécuter en même temps:
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.
Nous pouvons en exécuter un à la fois et revenir avec ces étapes:
Si je donne le nom de la cible dans une base de données vide, les étapes de modification ne s'exécuteront que jusque-là
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.
Bien sûr, je peux exécuter les manquants sans paramètre
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.
Et je peux me retirer sur une version particulière:
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.
Répéter
Felhasználó böngésző
SQL ADATBÁZIS ASP.NET MVC szerveralkalmazás (HTML/CSS/JavaScript)
+----------------------------+ +------------------------+ +------------------------+
| | | | | |
| | +--------------> | | +----------------> | |
| | | | | |
| | | | | |
| | <--------------+ | | <----------------+ | |
| | | | | |
| | | | | |
| | | +-------+ +------+ | | |
| | | | | | | | | |
| | | | | <+ | | | | |
| | <--------------+ | | | | | | | |
| | | | | | | | | |
| | | +-------+ +------+ | | |
+----------------------------+ +------------------------+ +------------------------+
^
|
Migrációs +
lépések Adatmodell
Lire: la loi des abstractions qui fuient
Nous créons une catégorie pour chaque aliment. Mais nous ne le mettons pas dans la table Menuems (c'est-à-dire la ligne MenuItem), mais le soulignons dans notre propre danse, ce qui le signifie à notre propre classe.
| Temps | Nom | Description | Prix | Catégorie |
|---|---|---|---|---|
| 1 | Trio de poissons de mer | Tatar du saumon atlantique avec filet de saumon mariné et thon à l'eau salée | 7500 | 1 |
| 3 | Veau | Boulettes | 4500 | 1 |
| Temps | Catégorie |
|---|---|
| 1 | Soupes, apéritifs |
Nous créons la classe de catégorie et un lien à partir de la classe MenuItem.
Ensuite, ces deux étapes suivent:
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
Pour afficher le menu
Informations LINQ avec cette recherche Google: Linq 101
| Catégorie. | Nom | Description | Prix |
|---|---|---|---|
| Apéritifs | Trio de poissons de mer | Tatar du saumon atlantique avec filet de saumon mariné et thon à l'eau salée | 7500 |
| Apéritifs | Veau | Boulettes | 4500 |
Opportunités:
Catégories
+-------------------------------------------------+ +---------------------------------------+
| | A választott+-----> | |
| | érték +---------------------------------------+
| | | |
| | | +--------------------------------+ |
| +----------------------+ | | |
| Name: | | | | +--------------------------------+ |
| +----------------------+ | A lehetséges | |
| +----------------------+ | értékek +-----> | +--------------------------------+ |
| Description: | | | felsorolása | |
| +----------------------+ | | +--------------------------------+ |
| +----------------------+ | | |
| Price: | | | | +--------------------------------+ |
| +----------------------+ | | |
| | | |
| | | |
| +----------------------+---+ | | |
| Category: | | | | | |
| +----------------------+---+ | | |
| | | |
| | | |
| | | |
| | | |
| +---------+ +---------+ | | |
| | Save | | Cancel | | | |
| +---------+ +---------+ | | |
| | +---------------------------------------+
| |
| |
| |
+-------------------------------------------------+
Exigence de données pour la déconnexion à afficher:
<-------------------------------------------------^
| |
Adatbázis | EntityFramework | Adatmodell
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
| | | | +----------------------------------+ | | Attach |
| | | | | DbContext.MenuItems | | <--------------------------^ |
| | | | +----------------------------------+ | | MenuItem | |
| | | | MenuItemEntry | | +------------------+-+ |
| | | | +----------------+ Entry | | | | |
| v | | | | <------------------------+ | |
| | | | | | | | | |
| Categories MenuItems | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | |
| | | | | | | | | | | | Category | |
| | | | | | | | | | | | +---------------+ | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | Category | <-----+ | MenuItem | | | | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | | | | | | +-+--------------+ | | | +---------------+ | |
| | | | | | | | | | | | |
| +-------------+ +-------------+ | | | Reference | | | | |
| | | | Load | | | | |
| | | | +-----------+ | | +--------------------+ |
| <----SaveChanges-------------+ +--------> | Category | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
La base de données et les «conversations» de la base de données peuvent être incluses avec le profileur SQL Server.
Démarrer SQL Server Profileer: SQL Server Management Studio Tools SQL Server Profileer
La gestion des données d'identité ASP.NET se compose de plusieurs couches:
+-------------------------------------------------------------+
| |
| |
| |
| |
| +-------------+ +--------------+ +-----------------+ |
| | | | | | | |
| | 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 >La raison de l'erreur est donc c'est
htmlattributes="{ class = form-control }" class="form-control"La solution consiste à copier le paramètre de la génération d'entrée de modification du terme précédent, où elle a été donnée en tant que paramètre AdditionalViewData . Mais @ html.dropdownListfor () attend un paramètre htmlattributes , qui est dans un format différent.
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes) sur system.web.mvc.html.selectextensions.dropdownlistfor [tmodel, tproperty] (htmlhelper`1 htmlhelper, expression`1 expression, ienumable`1 selectList, string optebel, idicction '1, ienumable`1 selectList System.web.mvc.html.selectextensions.dropdownListfor [TMODEL, TPropeerTy] (Htmlhelper`1 htmlhelper, expression`1 Expression, ienumeable`1 selectlist, objet htmlatributes) à ASP._Page_views_share repos ooprestaurant201807 ooprestaurant201807 vues shared editoremplates table.cshtml: ligne 19 (...) akkor a hiba oka az, hogy a lenyíló adattartalmát (jelen esetben AssignablesLocations nem inicializáltuk)
Différents points de validation possibles: valider l'application à chaque fois et protéger la base de données dans la mesure du possible!
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 | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | +----------------------------+ +------------------+ +------------+ | |
| | | |
| +-------------------------------------------------------------------------+ |
| |
+----------------------------------------------------------------------------------------------------------------------+
Si vous rencontrez une telle erreur après avoir téléchargé le code:
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
Cela peut être une solution:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Visual Studio Exit and Entry, puis reconstruire et mettre à jour-database.