Netacademia "Insight to Object -oried Programming on the 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) | | | | | | | |
| | | | | +--------------> | | | |
| +---------------+------+ | | +-----------------------+ |
| | | |
+----------------------------+ | |
| |
+-------------------------------------------+
هذه ليست مهمة تافهة على الإطلاق: تحديد الهوية وإدارة الأهلية
هوية ASP.NET يحلها بالنسبة لنا.
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
تُظهر الهوية حلاً مغريًا للغاية: أبدأ الكحول ولا تحتاج إلى أي تثبيت ، ويقوم بإنشاء قاعدة البيانات الخاصة به في الخلفية.
بطريقة ما ، لا ينشئ قاعدة البيانات تلقائيًا ، ولكن يكون لها تأثير عليها.
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
المتطلب السابق: وجود حزمة Nuget intityframework
الترخيص
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.
الخطوة تعسفية ، ودعت "هوية Datamodel" حتى أتمكن من تحديد هذه الخطوة لاحقًا.
أنشأ هذا الترحيل 201807050914249_IndIdity 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.
إذا قمت بتشغيل 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.
تحتوي أسماء الملفات في دليل الترحيل على خطوات تعديل قاعدة البيانات الموجودة في الكود. يحتوي جدول __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
نظرنا إلى قائمة مطعم 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.
قبل أن تحتاج إلى تحديث database :
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
| الفئة. اسم | اسم | وصف | سعر |
|---|---|---|---|
| المقبلات | ثلاثي الأسماك البحرية | سمك السلمون الأطلسي تاتار مع فيليه سمك السلمون المتبل والتونة مع الماء المالح | 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.
ابدأ SQL Server Profileer: 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"يتمثل الحل في نسخ المعلمة من توليد إدخال تحرير المصطلح السابق ، حيث تم إعطاؤه كمعلمة إضافية . ولكن @html.dropdownlistfor () يتوقع معلمة htmlattributes ، والتي هي في شكل مختلف.
1 selectList, Boolean allowMultiple, IDictionary 2 htmlattributes) في system.web.mvc.html.selectextensions.dropdownlistfor [tmodel ، tproperty] system.web.mvc.html.selectextensions.dropdownlistfor for [tmodel ، tpropeerty] (htmlhelper`1 htmlhelper ، expression`1 expression1 ، ienumerable`1 select () out in d: in d in d: in d in d in d in d stmlatributes at asp._page_views. repos ooprestaurant201807 ooprestaurant201807 views shared editortemplates table.cshtml: line 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 and Entry ، ثم إعادة البناء ، وتحديث database.