using System.Collections.Generic;
using

|
using System.Collections.Generic;
using
|
bet | 3/5 | Sana | 25.01.2024 | Hajmi | 0,79 Mb. | | #145486 |
Bog'liq Mavzu Web dasturlash Rejausing System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ContosoBooks.Models
{
public class Author
{
[ScaffoldColumn(false)]
public int AuthorID { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "First Name")]
public string FirstMidName { get; set; }
public virtual ICollection Books { get; set; }
}
}
Quyidagi kod bilan boshqa Book sinfini yaratish uchun ushbu amallarni takrorlang:
using System.ComponentModel.DataAnnotations;
namespace ContosoBooks.Models {
public class Book {
[ScaffoldColumn(false)]
public int BookID { get; set; }
[Required]
public string Title { get; set; }
public int Year { get; set; }
[Range(1, 500)]
public decimal Price { get; set; }
public string Genre { get; set; }
[ScaffoldColumn(false)]
public int AuthorID { get; set; }
// Navigation property
public virtual Author Author { get; set; }
}
}
Ilovani to'sib qo'ymaslik uchun har bir kitobda bitta muallif bo'lishi kerak. Author mulki muallif va kitob o'rtasidagi munosabatlar qanday boshqarilishini aniqlaydi. EFda ushbu turdagi mulk navigation property deb nomlanadi. EF ma'lumotlar bazasining sxemasini yaratganda, EF avtomatik ravishda AuthorID-ni kiritadi va bu Authors jadvalining ikkinchi darajali kaliti bo'ladi.
Iskala. Iskala yordamida o'rnatish sizning vaqtingiz va kuchingizni tejashga yordam beradi, bu sizning CRUD operatsiyalaringiz uchun avtomatik ravishda boshlang'ich nuqtani yaratadi. Oddiy model sinfidan boshlab va bitta kod satrini yozmasdan, siz ikkita kontroller yaratasiz, ularda mualliflar va kitoblar bilan bog'liq CRUD operatsiyalari, shuningdek zarur vakillar mavjud.
Iskala qo'shishni qo'shish uchun Solution Explorer dagi Controllers papkasini o'ng tugmasini bosing. Add–> New Scuffled Item-ni tanlang.
Izoh Agar siz New Scaffolded Item parametrini ko'rmasangiz, loyihani yaratishda Individual User Accounts-dan foydalanganingizga ishonch hosil qiling.
Add Scaffold dialog oynasida MVC 6 Controller with views, using Entity Framework-ni tanlang va so'ng Add tugmasini bosing.
Keyingi, Add Controller dialog oynasida namunaviy sinflar ro'yxatidan Book (ContosoBooks.Models) -ni tanlang. Bundan tashqari, kontekst sinfini ApplicationDbContext-ga o'rnating (ContosoBooks.Models). Generate views-da tasdiq belgisi o'rnatilishi kerak. Add tugmasini bosing.
Ko'rib turganingizdek, Add Controller dialog oynasi kontrollerlar va ko'rinishlar yaratish uchun variantlarni tanlashga imkon beradi.
Ushbu iskala boshqaruvchini va ko'rinishlar to'plamini ta'minlaydigan kodni yaratadi. Ko'rishlar ma'lumotlar bazasidan ma'lumotlarni yaratish, o'qish, yangilash, o'chirish va ro'yxatga olish uchun UI va kodga ega.
Author kontrollerini va u bilan bog'liq ko'rinishni yaratish uchun yuqoridagi iskala qadamlarini takrorlang. Quyidagi rasmda ko'rsatilgandek Author modeli sinfidan (ContosoBooks.Models) va ApplicationDbContext kontekst sinfidan (ContosoBooks.Models) foydalaning.
Solution Explorer-da siz Controller papkasiga yangi kontrollerlar va Views papkasida yangi ko'rinishlar qo'shilganligini ko'rasiz.
Ma'lumot qo'shmasdan oldin dasturni sozlash
Keyin dasturga Microsoft.Extensions.DependencyInjection paketini qo'shing. Solution Explorer-da, project.json-ni toping va oching. Bo'lim oxiridagi dependencies bo'limida ushbu qatorni yozing.
IntelliSense terayotganingizda sizga yordam beradi. Project.json-ni saqlaganingizda, Visual Studio avtomatik ravishda yangi paketga havolani ta'minlaydi.
Ushbu qatorni qo'shgandan so'ng, project.json faylidagi dependencies bo'limi quyidagicha bo'ladi:
"dependencies": {
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final"
},
Ma'lumotlar qo'shilmoqda. Biz ma'lumotlarni qo'l bilan kiritmaymiz - biz ma'lumotlar bazasini to'ldiradigan koddan foydalanamiz. Quyidagi kod bilan Models papkasida SampleData sinfini qo'shing:
|
| |