Netacademia "Insight Into Object -oriented Programming บนเว็บ: รหัสหลักสูตรร้านอาหาร" รหัสหลักสูตร
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 Identity แก้ปัญหาให้เรา
C# Project, ASP.NET Web Application (.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
Identity แสดงวิธีแก้ปัญหาที่น่าดึงดูดมาก: ฉันเริ่มต้นแอลกอฮอล์และคุณไม่จำเป็นต้องติดตั้งใด ๆ เขาสร้างฐานข้อมูลของตัวเองในพื้นหลัง
อย่างใดมันไม่ได้สร้างฐานข้อมูลโดยอัตโนมัติ แต่จะมีผลกับมัน
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.
ขั้นตอนนี้เป็นไปโดยพลการฉันเรียกว่า 'ข้อมูลประจำตัว DataModel' ดังนั้นฉันจึงสามารถระบุขั้นตอนนี้ได้ในภายหลัง
สิ่งนี้สร้าง การอพยพ 201807050914249_Identity dataModel.cs ไฟล์ (และไฟล์ทางเทคนิคอีกสองไฟล์)
สิ่งนี้เรียกว่า: ขั้นตอนการปรับเปลี่ยนขั้นตอนการโยกย้าย มีสองส่วนที่สำคัญ: ฟังก์ ชั่นขึ้น และ ลง ()
นำขั้นตอนการย้ายข้อมูลเข้าสู่ฐานข้อมูล
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.
ดังนั้นอุปกรณ์ของเรารู้รุ่นรุ่นฐานข้อมูลและรู้ว่าไม่มีอะไรขาดหายไป
คุณรู้สิ่งนี้ด้วยตาราง __migrationhistory
ชื่อไฟล์ในไดเรกทอรีการย้ายข้อมูลมีขั้นตอนการปรับเปลี่ยนฐานข้อมูลที่อยู่ในรหัส ตาราง __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 เป็นตัวย่อสำหรับพารามิเตอร์ TargetMigration และ 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 และมาต่อไปนี้:
ด้วยการสร้างโมเดลของเราเองเราสามารถเชื่อมต่อกับฐานข้อมูลประจำตัวโดยใช้คลาส ApplicatondBcontext ใน Models IdentityModels.cs
ถ้าฉันทำงานกับฐานข้อมูลที่พลาดขั้นตอนฉันไม่สามารถเพิ่มขั้นตอนการโยกย้ายอื่นได้:
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 | ปลาทะเลทั้งสาม | ทาตาร์ปลาแซลมอนแอตแลนติกกับเนื้อปลาแซลมอนหมักและปลาทูน่าด้วยน้ำเกลือ | 7500 | 1 |
| 3 | น่อง | เกี๊ยว | 4500 | 1 |
| เวลา | หมวดหมู่ |
|---|---|
| 1 | ซุปอาหารทานเล่น |
เราสร้างคลาสหมวดหมู่และลิงค์จากคลาส Menuitem
จากนั้นสองขั้นตอนนี้ตาม:
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
เพื่อแสดงเมนู
ข้อมูล LINQ ด้วยการค้นหาของ Google นี้: LINQ 101
| category.name | ชื่อ | คำอธิบาย | ราคา |
|---|---|---|---|
| อาหารเรียกน้ำย่อย | ปลาทะเลทั้งสาม | ทาตาร์ปลาแซลมอนแอตแลนติกกับเนื้อปลาแซลมอนหมักและปลาทูน่าด้วยน้ำเกลือ | 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 | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
ฐานข้อมูลและ EntityFramework "การสนทนา" สามารถรวมอยู่ใน SQL Server Profiler
เริ่มโปรไฟล์ SQL Server: SQL Server Management Studio Tools SQL Server Profileer
การจัดการข้อมูลข้อมูลประจำตัว 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"วิธีแก้ปัญหาคือการคัดลอกพารามิเตอร์จากการสร้างอินพุตแก้ไขคำก่อนหน้านี้ซึ่งได้รับเป็นพารามิเตอร์ เพิ่มเติม ViewData แต่ @html.dropdownlistfor () คาดว่าจะมีพารามิเตอร์ HTMLattributes ซึ่งอยู่ในรูปแบบที่แตกต่างกัน
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes) ที่ system.web.mvc.html.selectextensions.dropdownlistfor [tmodel, tproperty] (htmlhelper`1 htmlhelper System.web.mvc.html.selectextensions.dropdownListfor [tmodel, tpropeerty] (htmlhelper`1 htmlhelper, นิพจน์ 'นิพจน์, ienumerable`1 selectlist, วัตถุ htmlatributes) ที่ ASP._PAGE_Views_Views_views () ใน D: repos Ooprestaurant201807 Ooprestaurant201807 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 จากนั้นสร้างใหม่และอัปเดต DATABASE