NetAcademia“洞察网络上面向对象的编程:餐厅项目”课程代码
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身份为我们解决。
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
先决条件:实体Framework Nuget Pack的存在
许可
PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project OopRestaurant201807.
这创建了迁移 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.
该步骤是任意的,我称为“身份数据模型”,因此我可以稍后确定此步骤。
这创建了迁移 201807050914249_Identity datamodel.cs文件(和两个更多技术文件)
这就是称为:修改步骤,迁移步骤。有两个重要的部分: 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.
如果您再次运行更新数据库,请写下:
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.
因此,我们的设备知道模型版本,数据库版本,并且知道什么都没有丢失。
您在__移民史表中知道这一点。
迁移目录中的文件名包含代码中的数据库修改步骤。 __迁移史表包含数据库中的步骤。
所有提款(重置为版本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
我们查看了Gundel餐厅的菜单,并介绍了以下内容:
通过创建我们自己的模型,我们可以通过使用 models sidentitymodels.cs中的applicatondBcontext类连接到身份数据库。
如果我使用错过步骤的数据库,我将无法添加另一个迁移步骤:
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.
我们可以一次运行一个步骤:
如果我在一个空数据库中命名目标的名称,则修改步骤只能在此之前运行
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系列),而是在我们自己的舞蹈中强调它,这意味着它在我们自己的班级上。
| 时间 | 姓名 | 描述 | 价格 | 类别 |
|---|---|---|---|---|
| 1 | 海鱼三重奏 | 大西洋鲑鱼塔塔(Tatar | 7500 | 1 |
| 3 | 小牛 | 饺子 | 4500 | 1 |
| 时间 | 类别 |
|---|---|
| 1 | 汤,开胃菜 |
我们创建类别类别和Menuitem类的链接。
然后以下两个步骤:
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
显示菜单
使用此Google搜索的LINQ信息: LINQ 101
| 类别。名称 | 姓名 | 描述 | 价格 |
|---|---|---|---|
| 开胃菜 | 海鱼三重奏 | 大西洋鲑鱼塔塔(Tatar | 7500 |
| 开胃菜 | 小牛 | 饺子 | 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 | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
SQL Server Profiler可以包含数据库和EntityFramework“对话”。
启动SQL Server Profile:SQL Server Management Studio Tools SQL Server Profile
ASP.NET身份数据管理由几层组成:
+-------------------------------------------------------------+
| |
| |
| |
| |
| +-------------+ +--------------+ +-----------------+ |
| | | | | | | |
| | 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"解决方案是从上一个术语编辑输入生成中复制参数,并以附加视图Data参数给出。但是 @html.dropdownlistfor()期望以不同格式的htmlattributes参数。
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes)in system.web.mvc.html.selectextensions.dropdownlistfor [tmodel,tproperty](tproperty](tproperty])(htmlhelper'1 System.web.mvc.html.selectextensions.dropdownlistfor [tmodel, tpropeerty] (htmlhelper`1 htmlhelper, expression`1 expression, ienumerable`1 Selectlist, Object htmlattributes) at ASP._PAGE_VIEWS_SHARED_EDITORTEMPLTES_TABLE_CSHTML.EXECUTE ()在d: repos ooprestaurant201807 ooprestaurant201807 view shared 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退出和进入,然后重建和更新数据库。