• Company
  • Entity Framework
  • EF DbUpdateConcurrencyException
  • Dasturiy injiniring (EntityFramework 6)




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

    Тashqi kalitni o’rnatish
    Bir modeldagi xususiyatni boshqa model xususiyatiga ikkilamchi kalitligini o’rnatish uchun ForeignKey atributidan foydalaniladi:

    public class Phone


    {
    public int Id { get; set; }
    public string Name { get; set; }

    public int? CompId { get; set; }


    [ForeignKey("CompId")]
    public Company Company { get; set; }
    }

    public class Company


    {
    public int Id { get; set; }
    public string Name { get; set; }
    }

    Ushbu holda Company modeli bilan tashqi kalit vazifasini CompId xususiyati bajaradi.




    ConcurrencyCheck
    ConcurrencyCheck atributi orqali parallellashtirish muammosi hal qilinadi va jadval ustida bir nechta foydalanuvchilar amallarni parallel bajarishlari mumkin:

    public class Phone


    {
    public int Id { get; set; }
    [ConcurrencyCheck]
    public string Name { get; set; }
    }

    Ikkita foydalanuvchi bitta qiymatni o’zgartirishlari mumkin:


    using (FluentContext db = new FluentContext())


    {
    Phone phone = db.Phones.Find(1);
    phone.Name = "Nokia N9";
    db.Entry(phone).State = EntityState.Modified;
    db.SaveChanges();
    }

    An’anaviy holda Entity Framework yangilash jarayonida jadval va model Id si mos bo’lsa, jadvaldagi satr yangilanadi. ConcurrencyCheck atributini qo’llash orqali EF Id va Name xususiyatlar qiymatlari mosligini ham tekshiradi. Agar ushbu qiymatlar o’zaro mos bo’lsa, yozuv yangilanadi. Agar qiymatlar mos bo’lmasa, EF DbUpdateConcurrencyException xatolikni qaytaradi.


    Kompleks tiplar ustida amallar


    Entity Framework da har bir model alohida jadvalga mos qo’yiladi. Ammo ba’zi hollarda muayyan klass modelning qo’shimcha ma’lumotlarini o’zida saqlashi mumkin. Masalan:

    public class PhoneInfo


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

    public class Phone


    {
    public int Id { get; set; }
    public string Name { get; set; }

    public PhoneInfo Info { get; set; }


    public Phone()


    {
    Info = new PhoneInfo { Price = 400000 };
    }
    }



    Download 1,39 Mb.
    1   ...   43   44   45   46   47   48   49   50   ...   53




    Download 1,39 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Dasturiy injiniring (EntityFramework 6)

    Download 1,39 Mb.