Netacademia "Web上のオブジェクト指向プログラミングに関する洞察:レストランプロジェクト"コースコード
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) | | | | | | | |
| | | | | +--------------> | | | |
| +---------------+------+ | | +-----------------------+ |
| | | |
+----------------------------+ | |
| |
+-------------------------------------------+
これは些細な作業ではありません:識別と適格性管理
ASP.NET IDは私たちのためにそれを解決します。
C#プロジェクト、ASP.NET Webアプリケーション(.NETフレームワーク):

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
アイデンティティは非常に魅力的な解決策を示しています。私はアルコールを開始し、インストールは必要ありません。彼はバックグラウンドで独自のデータベースを作成します。
どういうわけか、それは自動的にデータベースを作成するのではなく、それに影響を与えるためです。
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
前提条件:EntityFramework Nuget Packの存在
ライセンス
PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project OopRestaurant201807.
これにより、Migrations Configuration.csステーションが作成されました。
アイデンティティモデルを移行ステップに呼びかける
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.
ステップはarbitrary意的で、 「IDデータモデル」と呼ばれるので、後でこのステップを特定できました。
これにより、移行 201807050914249_IDENTITY DATAMODEL.CSファイルが作成されました(およびさらに2つの技術ファイル)
これは、変更ステップ、移行ステップと呼ばれます。 2つの重要な部分があります: up()とdown()関数。
データベースへの移行ステップの配置
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.
最初に実行されません。その理由は、 Web.configに探しているデータアクセスパスがあるためです。私はすでにこのMDFファイルを削除しているので、そのようなことはありませんので、迷子になります。 web.configからデータ接続設定を削除すると、問題なく実行されます。
このデータベースを作成します。

独自のマシンのデータベースは、デフォルトのSQLインスタンスに定義され、 DefaultConnectionと呼ばれます。
https://www.connectionstings.com/で接続設定を作成します。
< connectionStrings >
< add name = " DefaultConnection " connectionString = " Server=.SQLEXPRESS;Database=OopRestaurantDb;Trusted_Connection=True "
providerName = " System.Data.SqlClient " />
</ connectionStrings >ユーザーがログインするときに、サーバー名、データベース名、およびメソッドを入力することが重要です。
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.
更新Databaseをもう一度実行すると、これを書きます。
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.
そのため、デバイスはモデルバージョンであるデータベースバージョンを知っており、何も欠落していないことを知っています。
これは__ MigrationHistoryテーブルで知っています。
Migrationsディレクトリのファイル名には、コードにあるデータベースの変更手順が含まれています。 __migrationhistoryテーブルには、データベースの手順が含まれています。
すべての引き出し(バージョン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はターゲット移行パラメーターの略語であり、0は各ステップの前の状態です
PM> update-database -Script
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
-Scriptパラメーターは実行されませんが、移行ステップによって生成されたSQLスクリプトが表示されます。
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
ガンデルレストランのメニューを見て、次のようになりました。
独自のモデルを作成することにより、 Models IdentityModels.csのApplicatondBContextクラスを使用して、IDデータベースに接続できます。
手順を逃すデータベースを使用して作業した場合、別の移行ステップを追加できません。
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.
更新データを必要とする前に:
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.
メモは、まだ変更スクリプトにないモデルの変更があるため、データベースをモデルと一致させることはできないということです。ただし、これは単なる黄色の警告であり、データベースへの移行ステップを再生するため、モデルの変更から次のデータベースの変更を作成できます。
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.
次に、データベースを使用してデータベースを更新できます。
一連のステップを完全に引き出したり、同時に実行したりできます。
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.
これらの手順で一度に1つずつ実行できます。
空のデータベースでターゲットの名前を付けると、変更手順はそれまでのみ実行されます
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.
もちろん、私はパラメーターなしで欠落しているものを実行できます
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.
そして、私は特定のバージョンに撤回することができます:
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.
繰り返す
Felhasználó böngésző
SQL ADATBÁZIS ASP.NET MVC szerveralkalmazás (HTML/CSS/JavaScript)
+----------------------------+ +------------------------+ +------------------------+
| | | | | |
| | +--------------> | | +----------------> | |
| | | | | |
| | | | | |
| | <--------------+ | | <----------------+ | |
| | | | | |
| | | | | |
| | | +-------+ +------+ | | |
| | | | | | | | | |
| | | | | <+ | | | | |
| | <--------------+ | | | | | | | |
| | | | | | | | | |
| | | +-------+ +------+ | | |
+----------------------------+ +------------------------+ +------------------------+
^
|
Migrációs +
lépések Adatmodell
読む:抽象化を漏らす法則
各食品のカテゴリを作成します。しかし、私たちはそれをMenuitemsのテーブル(つまり、Menuitem Line)に入れませんが、私たち自身のダンスでそれを強調します。
| 時間 | 名前 | 説明 | 価格 | カテゴリ |
|---|---|---|---|---|
| 1 | シーフィッシュトリオ | マリネしたサーモンの切り身と塩水とマグロを添えた大西洋サーモンタタール | 7500 | 1 |
| 3 | カーフ | dumplings | 4500 | 1 |
| 時間 | カテゴリ |
|---|---|
| 1 | スープ、前菜 |
Menuitemクラスからカテゴリクラスとリンクを作成します。
次に、これらの2つのステップが続きます。
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
メニューを表示します
このGoogle検索でのLINQ情報: LINQ 101
| category.name | 名前 | 説明 | 価格 |
|---|---|---|---|
| 前菜 | シーフィッシュトリオ | マリネしたサーモンの切り身と塩水とマグロを添えた大西洋サーモンタタール | 7500 |
| 前菜 | カーフ | dumplings | 4500 |
機会:
カテゴリ
+-------------------------------------------------+ +---------------------------------------+
| | A választott+-----> | |
| | érték +---------------------------------------+
| | | |
| | | +--------------------------------+ |
| +----------------------+ | | |
| Name: | | | | +--------------------------------+ |
| +----------------------+ | A lehetséges | |
| +----------------------+ | értékek +-----> | +--------------------------------+ |
| Description: | | | felsorolása | |
| +----------------------+ | | +--------------------------------+ |
| +----------------------+ | | |
| Price: | | | | +--------------------------------+ |
| +----------------------+ | | |
| | | |
| | | |
| +----------------------+---+ | | |
| Category: | | | | | |
| +----------------------+---+ | | |
| | | |
| | | |
| | | |
| | | |
| +---------+ +---------+ | | |
| | Save | | Cancel | | | |
| +---------+ +---------+ | | |
| | +---------------------------------------+
| |
| |
| |
+-------------------------------------------------+
切断が表示されるためのデータ要件:
<-------------------------------------------------^
| |
Adatbázis | EntityFramework | Adatmodell
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
| | | | +----------------------------------+ | | Attach |
| | | | | DbContext.MenuItems | | <--------------------------^ |
| | | | +----------------------------------+ | | MenuItem | |
| | | | MenuItemEntry | | +------------------+-+ |
| | | | +----------------+ Entry | | | | |
| v | | | | <------------------------+ | |
| | | | | | | | | |
| Categories MenuItems | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | |
| | | | | | | | | | | | Category | |
| | | | | | | | | | | | +---------------+ | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | Category | <-----+ | MenuItem | | | | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | | | | | | +-+--------------+ | | | +---------------+ | |
| | | | | | | | | | | | |
| +-------------+ +-------------+ | | | Reference | | | | |
| | | | Load | | | | |
| | | | +-----------+ | | +--------------------+ |
| <----SaveChanges-------------+ +--------> | Category | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
データベースとEntityFrameworkの「会話」は、SQL Server Profilerに含めることができます。
SQL Serverプロファイルを開始:SQL Server Management Studio Tools SQL Serverプロファイル
ASP.NET IDデータ管理は、いくつかのレイヤーで構成されています。
+-------------------------------------------------------------+
| |
| |
| |
| |
| +-------------+ +--------------+ +-----------------+ |
| | | | | | | |
| | 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 >したがって、エラーの理由はこれです
htmlattributes="{ class = form-control }" class="form-control"解決策は、追加の用語編集入力生成からパラメーターをコピーすることです。ただし、 @html.dropdownListfor()は、異なる形式のhtmlattributesパラメーターを期待しています。
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes)at System.web.mvc.html.selectextensions.dropdownListfor [tmodel、tproperty](htmlhelper`1 htmlhelper、発現 `1発現、イネマイマイマイナブル` 2セレクトリスト、IdeNumerableaid `2 seartibel、string` 1 htmlhelperper System.web.mvc.html.selectextensions.dropdownListfor [tmodel、tpropeerty](htmlhelper`1 htmlhelper、expression`1 expression、ienumerable`1 selectlist、object htmlatributes) in d: repos oooprestaurant201807 oooprestaurant201807 views shared editortemplates table.cshtml:行19(...) akkor a hiba oka az, hogy a lenyíló adattartalmát (jelen esetben AssignablesLocations nem inicializáltuk)
検証の可能性のあるさまざまなポイント:毎回アプリケーションを有効にし、可能な場合はデータベースを保護します!
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 | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | +----------------------------+ +------------------+ +------------+ | |
| | | |
| +-------------------------------------------------------------------------+ |
| |
+----------------------------------------------------------------------------------------------------------------------+
コードをダウンロードした後にそのようなエラーに遭遇した場合:
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
これは解決策かもしれません:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Visual Studio ExitとEntry、再構築、および更新Database。