• Phone
  • ComplexType
  • Ikkita modeldan yagona jadvalda foydalanish
  • Dasturiy injiniring (EntityFramework 6)




    Download 1,39 Mb.
    bet48/53
    Sana30.11.2023
    Hajmi1,39 Mb.
    #108476
    1   ...   45   46   47   48   49   50   51   52   53
    Bog'liq
    Dasturiy injiniring

    PhoneInfo klassi Phone modeliga nisbatan bir nechta qo’shimcha ma’lumotni o’zida saqlaydi. Phone modeli PhoneInfo ob’ektini boshlang‘ich qiymatlar bilan hosil qiladi. Ushbu tarzda shakllantirilgan tiplar kompleksli tiplar deb yuritiladi.
    Kompleks tiplar ustida bir qator cheklagichlar mavjud:

    • ularda kalitlar mavjud emas;

    • ular o’zida oddiy tiplarga mansub xususiyatlarni saqlaydi;

    • ushbu turdagi modellarda kolleksiyalardan foydalanib bo’lmaydi. Ya’ni Phone modeli PhoneInfo kolleksiyasini o’zida saqlay olmaydi.



    Phone va PhoneInfo tiplar o'rtasida aloqani tashkil qilish uchun ComplexType atributidan foydalanish lozim. Ushbu atribut orqali turli klasslardan yagona element hosil qilinadi. Misol:

    [ComplexType]


    public class PhoneInfo
    {
    public string Company { get; set; }
    public int Price { get; set; }
    }


    Phone klassida hech qanday o’zgarish amalga oshirilmaydi. Yuqoridagi tarzda tashkil qilingan tiplarni quyidagicha ishlatish mumkin:

    using (FluentContext db = new FluentContext())


    {
    db.Phones.Add(new Phone
    {
    Name = "Samsung Galaxy S5",
    Info = new PhoneInfo { Company = "Samsung", Price = 17000 }
    });
    db.Phones.Add(new Phone
    {
    Name = "Nokia Lumia 930",
    Info = new PhoneInfo { Company = "Nokia", Price = 15000 }
    });
    db.SaveChanges();
    foreach (Phone p in db.Phones)
    Console.WriteLine("{0} - {1}", p.Name, p.Info.Price);
    }

    Ikkita klass orqali hosil qilinayotgan jadval yagona element sifatida shakllantiriladi:



    ComplexType metodidan FluentAPI da foydalanib atributlarni shakllantirish quyidagicha:

    class FluentContext : DbContext


    {
    public FluentContext()
    : base("DefaultConnection")
    { }
    public DbSet Phones { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)


    {
    modelBuilder.ComplexType();
    base.OnModelCreating(modelBuilder);
    }
    }

    Ikkita modeldan yagona jadvalda foydalanish


    Avvalgi mavzuda ikkita klass yagona element sifatida bitta jadvalda saqlangan edi. Endi boshqa modelni ko’rib chiqamiz. Ikkita model o’zaro bog‘langan bo’lib, ularni yagona jadvalga saqlash uchun o'lar o’zaro birga-bir bog‘lanishga ega bo’lishi lozim:

    using System;


    using System.Text;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace FluentAPIApp


    {
    [Table("Mobiles")]
    public class PhoneInfo
    {
    [Key, ForeignKey("Phone")]
    public int PhoneId { get; set; }
    public string Company { get; set; }
    public int Price { get; set; }

    public Phone Phone { get; set; }


    }

    [Table("Mobiles")]


    public class Phone
    {
    [Key, ForeignKey("Info")]
    public int PhoneId { get; set; }
    public string Name { get; set; }

    public PhoneInfo Info { get; set; }


    }

    class MobileContext : DbContext


    {
    public MobileContext()
    : base("DefaultConnection")
    { }

    public DbSet Phones { get; set; }


    public DbSet Infos { get; set; }
    }
    }

    Download 1,39 Mb.
    1   ...   45   46   47   48   49   50   51   52   53




    Download 1,39 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Dasturiy injiniring (EntityFramework 6)

    Download 1,39 Mb.