• O’zgartirish
  • Dasturiy injiniring (EntityFramework 6)




    Download 1,39 Mb.
    bet21/53
    Sana30.11.2023
    Hajmi1,39 Mb.
    #108476
    1   ...   17   18   19   20   21   22   23   24   ...   53
    Bog'liq
    Dasturiy injiniring

    Birga-ko’p munosabat
    Birga ko’p munosabat orqali tashkil qilingan modelda, bitta modelda boshqa model ob’ektiga uzatma mavjud bo’lsa, ikkinchi model esa birinchi model ob’ektlari kolleksiyasiga uzatmani saqlashi lozim. Misol, bitta jamoada bir qancha futbolchilar mavjud bo’lgan holda:

    class Player


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

    public int? TeamId { get; set; }


    public Team Team { get; set; }
    }

    class Team


    {
    public int Id { get; set; }
    public string Name { get; set; } // jamoa nomi
    public ICollection Players { get; set; }
    public Team()
    {
    Players = new List();
    }
    }

    class SoccerContext : DbContext


    {
    public SoccerContext()
    : base("SoccerContext")
    { }

    public DbSet Players { get; set; }


    public DbSet Teams { get; set; }
    }

    Ushbu klasslarga mos holda quyidagi amallarni ko’rib chiqamiz.


    Yangi yozuv qo’shish va olish:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.Entity;

    namespace OneToMoreApp


    {
    class Program
    {
    static void Main(string[] args)
    {
    using (SoccerContext db = new SoccerContext())
    {
    // modellarni yaratish va qo’shish
    Team t1 = new Team { Name = "Barselona" };
    Team t2 = new Team { Name = "Real Madrid" };
    db.Teams.Add(t1);
    db.Teams.Add(t2);
    db.SaveChanges();
    Player pl1 = new Player { Name = "Ronaldu", Age = 31, Position = "Hujumchi", Team = t2 };
    Player pl2 = new Player { Name = "Messi", Age = 28, Position = "Hujumchi", Team = t1 };
    Player pl3 = new Player { Name = "Xavi", Age = 34, Position = "Yarim himoyachi", Team = t1 };
    db.Players.AddRange(new List { pl1, pl2, pl3 });
    db.SaveChanges();

    // konsolga chiqarish


    foreach (Player pl in db.Players.Include(p => p.Team))
    Console.WriteLine("{0} - {1}", pl.Name, pl.Team != null ? pl.Team.Name : "");
    Console.WriteLine();
    foreach (Team t in db.Teams.Include(t => t.Players))
    {
    Console.WriteLine("Komanda: {0}", t.Name);
    foreach (Player pl in t.Players)
    {
    Console.WriteLine("{0} - {1}", pl.Name, pl.Position);
    }
    Console.WriteLine();
    }
    }

    }
    }
    }




    O’zgartirish:
    // o’zgartirish
    t2.Name = "Реал М."; // jamoa nomini o’zgartiramiz
    db.Entry(t2).State = EntityState.Modified;
    // bir futbolchini boshqa jamoga o'tkazamiz
    pl3.Team = t2;
    db.Entry(pl3).State = EntityState.Modified;
    db.SaveChanges();


    O’chirish:
    // O’chirish
    Player pl_toDelete = db.Players.First(p => p.Name == "Ronaldu");
    db.Players.Remove(pl_toDelete);
    // Komandani o’chirish
    Team t_toDelete = db.Teams.First();
    db.Teams.Remove(t_toDelete);
    db.SaveChanges();



    Download 1,39 Mb.
    1   ...   17   18   19   20   21   22   23   24   ...   53




    Download 1,39 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Dasturiy injiniring (EntityFramework 6)

    Download 1,39 Mb.