NetAcademia "Wawasan tentang Pemrograman Berorientasi Objek di Web: The Restaurant Project" Kode Kursus
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) | | | | | | | |
| | | | | +--------------> | | | |
| +---------------+------+ | | +-----------------------+ |
| | | |
+----------------------------+ | |
| |
+-------------------------------------------+
Ini bukan tugas sepele sama sekali: manajemen identifikasi dan kelayakan
Identitas ASP.NET menyelesaikannya untuk kita.
Proyek C#, Aplikasi 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
Identitas menunjukkan solusi yang sangat menggoda: Saya memulai alkohol dan Anda tidak memerlukan instalasi apa pun, ia membuat basis data sendiri di latar belakang.
Entah bagaimana, itu tidak secara otomatis membuat database, tetapi memiliki efek padanya.
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
Prasyarat: Keberadaan Paket Nuget EntityFramework
lisensi
PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project OopRestaurant201807.
Ini menciptakan stasiun migrasi configuration.cs .
Untuk meminta model identitas ke dalam langkah migrasi
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.
Langkahnya sewenang -wenang, saya menyebut 'identitas datamodel' sehingga saya bisa mengidentifikasi langkah ini nanti.
Ini menciptakan migrasi 201807050914249_identity file datamodel.cs (dan dua file teknis lainnya)
Ini disebut: langkah modifikasi, langkah migrasi. Ada dua bagian penting: UP () dan Down () fungsi.
Memasukkan langkah migrasi ke dalam basis data
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.
Itu tidak berjalan terlebih dahulu, alasannya adalah Web.config memiliki jalur akses data yang Anda cari. Saya sudah menghapus file MDF ini, jadi tidak ada hal seperti itu sehingga hilang. Jika Anda menghapus pengaturan koneksi data dari web.config , itu akan berjalan tanpa masalah.
Membuat database ini:

Basis data pada mesinnya sendiri didefinisikan ke instance SQL default dan disebut DefaultConnection .
Kami membuat pengaturan koneksi dengan https://www.connectionstings.com/.
< connectionStrings >
< add name = " DefaultConnection " connectionString = " Server=.SQLEXPRESS;Database=OopRestaurantDb;Trusted_Connection=True "
providerName = " System.Data.SqlClient " />
</ connectionStrings >Penting untuk memasukkan nama server, nama basis data, dan metode sebagai pengguna masuk.
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.
Jika Anda menjalankan pembaruan-database lagi, Anda menulis ini:
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.
Jadi, perangkat kami mengetahui versi model, versi basis data, dan tahu bahwa tidak ada yang hilang.
Anda tahu ini dengan tabel __migrationHistory.
Nama file dalam direktori migrasi berisi langkah -langkah modifikasi basis data yang ada dalam kode. Tabel __migrationHistory berisi langkah -langkah dalam database.
Semua penarikan (reset ke versi 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 adalah singkatan untuk parameter targetmigrasi dan 0 adalah keadaan sebelum setiap langkah
PM> update-database -Script
Applying explicit migrations: [201807050914249_Identity datamodel].
Applying explicit migration: 201807050914249_Identity datamodel.
Parameter -Script tidak berjalan ke bawah, tetapi itu menunjukkan kepada kita skrip SQL yang dihasilkan oleh langkah migrasi.
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
Kami melihat menu Gundel Restaurant dan datang ke yang berikut:
Dengan membuat model kami sendiri, kami dapat menghubungkannya ke database identitas dengan menggunakan kelas ApplicatondBContext di model identitymodels.cs.
Jika saya bekerja dengan database yang melewatkan langkah -langkah, saya tidak dapat menambahkan langkah migrasi lain:
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.
Sebelum Anda membutuhkan pembaruan-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.
Catatannya adalah bahwa Anda tidak dapat membawa database sesuai dengan model kami karena ada perubahan model yang belum ada dalam skrip perubahan. Namun, ini hanya peringatan kuning, memainkan langkah -langkah migrasi ke dalam database sehingga kami dapat membuat perubahan database berikutnya dari perubahan model.
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.
Kemudian Anda dapat memperbarui database dengan pembaruan database.
Kita dapat menarik serangkaian langkah lengkap atau menjalankannya pada saat yang sama:
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.
Kita dapat menjalankan satu per satu dan kembali dengan langkah -langkah ini:
Jika saya memberikan nama target dalam database kosong, langkah -langkah modifikasi hanya akan berjalan sampai saat itu
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.
Tentu saja saya bisa menjalankan yang hilang tanpa parameter
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.
Dan saya dapat menarik diri ke versi tertentu:
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.
Mengulang
Felhasználó böngésző
SQL ADATBÁZIS ASP.NET MVC szerveralkalmazás (HTML/CSS/JavaScript)
+----------------------------+ +------------------------+ +------------------------+
| | | | | |
| | +--------------> | | +----------------> | |
| | | | | |
| | | | | |
| | <--------------+ | | <----------------+ | |
| | | | | |
| | | | | |
| | | +-------+ +------+ | | |
| | | | | | | | | |
| | | | | <+ | | | | |
| | <--------------+ | | | | | | | |
| | | | | | | | | |
| | | +-------+ +------+ | | |
+----------------------------+ +------------------------+ +------------------------+
^
|
Migrációs +
lépések Adatmodell
Membaca: Hukum abstraksi bocor
Kami membuat kategori untuk setiap item makanan. Tapi kami tidak memasukkannya ke dalam tabel Menuitem (yaitu, garis Menuitem), tetapi menekankannya dalam tarian kita sendiri, yang berarti bagi kelas kita sendiri.
| Waktu | Nama | Keterangan | Harga | Kategori |
|---|---|---|---|---|
| 1 | Trio ikan laut | Atlantik salmon tatar dengan fillet salmon yang diasinkan dan tuna dengan air asin | 7500 | 1 |
| 3 | Anak sapi | Pangsit | 4500 | 1 |
| Waktu | Kategori |
|---|---|
| 1 | Sup, makanan pembuka |
Kami membuat kelas kategori dan tautan dari kelas Menuitem.
Kemudian kedua langkah ini mengikuti:
PM> add-migration 'add Category table, and MenuItem.Category column'
PM> update-database
Untuk menampilkan menu
Informasi LINQ dengan pencarian Google ini: LINQ 101
| Kategori.name | Nama | Keterangan | Harga |
|---|---|---|---|
| Makanan pembuka | Trio ikan laut | Atlantik salmon tatar dengan fillet salmon yang diasinkan dan tuna dengan air asin | 7500 |
| Makanan pembuka | Anak sapi | Pangsit | 4500 |
Peluang:
Kategori
+-------------------------------------------------+ +---------------------------------------+
| | A választott+-----> | |
| | érték +---------------------------------------+
| | | |
| | | +--------------------------------+ |
| +----------------------+ | | |
| Name: | | | | +--------------------------------+ |
| +----------------------+ | A lehetséges | |
| +----------------------+ | értékek +-----> | +--------------------------------+ |
| Description: | | | felsorolása | |
| +----------------------+ | | +--------------------------------+ |
| +----------------------+ | | |
| Price: | | | | +--------------------------------+ |
| +----------------------+ | | |
| | | |
| | | |
| +----------------------+---+ | | |
| Category: | | | | | |
| +----------------------+---+ | | |
| | | |
| | | |
| | | |
| | | |
| +---------+ +---------+ | | |
| | Save | | Cancel | | | |
| +---------+ +---------+ | | |
| | +---------------------------------------+
| |
| |
| |
+-------------------------------------------------+
Persyaratan Data untuk Putuskan Terpisah:
<-------------------------------------------------^
| |
Adatbázis | EntityFramework | Adatmodell
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
| | | | +----------------------------------+ | | Attach |
| | | | | DbContext.MenuItems | | <--------------------------^ |
| | | | +----------------------------------+ | | MenuItem | |
| | | | MenuItemEntry | | +------------------+-+ |
| | | | +----------------+ Entry | | | | |
| v | | | | <------------------------+ | |
| | | | | | | | | |
| Categories MenuItems | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | |
| | | | | | | | | | | | Category | |
| | | | | | | | | | | | +---------------+ | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | Category | <-----+ | MenuItem | | | | | | | | | | | |
| +-------------+ +-------------+ | | | | | | | | | | |
| | | | | | | +-+--------------+ | | | +---------------+ | |
| | | | | | | | | | | | |
| +-------------+ +-------------+ | | | Reference | | | | |
| | | | Load | | | | |
| | | | +-----------+ | | +--------------------+ |
| <----SaveChanges-------------+ +--------> | Category | | | |
| | | +-----------+ | | |
+---------------------------------------------+ +---------------------------------------+ +-----------------------------+
Database dan EntityFramework "Conversations" dapat disertakan dengan SQL Server Profiler.
Mulai SQL Server Profileer: SQL Server Management Studio Tools SQL Server Profileer
Asp.net Manajemen data identitas terdiri dari beberapa lapisan:
+-------------------------------------------------------------+
| |
| |
| |
| |
| +-------------+ +--------------+ +-----------------+ |
| | | | | | | |
| | 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 >Alasan kesalahannya adalah karena ini
htmlattributes="{ class = form-control }" class="form-control"Solusinya adalah menyalin parameter dari istilah sebelumnya mengedit input menghasilkan, di mana itu diberikan sebagai parameter tambahanviewData . Tapi @html.dropdownlistfor () mengharapkan parameter htmlattributes , yang berada dalam format yang berbeda.
1 selectList, Boolean allowMultiple, IDictionary 2 Htmlattributes) At System.web.mvc.html.selectextensions.dropdownlistfor [TMODEL, TPROPERTY] (htmlhelper`1 htmlhelper, expression`1 expression, ienumerable`1 Selectlist, String OptiBel, Idicctionary`2 System.web.mvc.html.selectextensions.dropDownlist untuk [tmodel, tpropeerTy] (htmlhelper`1 htmlhelper, ekspresi`1 ekspresi, ienumerable`1 scareCare_crare.semple.pareCeDe.pareClare_clatributes) di asp._page_page_shiews_shiews_share.sshare.shareCeREDE) di ASP._Page_VIEWS_SHOWSES_SHECHS_SHECHECEDECECEDECEDED) di ASP._PAGES_VIEWS_SHECHS_SHECHS_SHECHECEDECECLECEDECEDE yang ada yang ” () dalam D: repos ooprestaurant201807 ooprestaurant201807 views shared editortemplates table.cshtml: baris 19 (...) akkor a hiba oka az, hogy a lenyíló adattartalmát (jelen esetben AssignablesLocations nem inicializáltuk)
Berbagai poin validasi yang berbeda: valid aplikasi setiap waktu dan lindungi database jika memungkinkan!
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 | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | +----------------------------+ +------------------+ +------------+ | |
| | | |
| +-------------------------------------------------------------------------+ |
| |
+----------------------------------------------------------------------------------------------------------------------+
Jika Anda mengalami kesalahan seperti itu setelah mengunduh kode:
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
Ini mungkin solusi:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Visual Studio keluar dan masuk, lalu membangun kembali, dan memperbarui-database.