lazy loading - Dasturiy injiniring (EntityFramework 6)

lazy loading ga ko’ra jamoa va unga mos o'yinchilarga ega bo’lishimiz va ularning xususiyatlariga murojaat qilishimiz mumkin.
Jamoa formasi kodi
:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OneToMore
{
public partial class TeamForm : Form
{
SoccerContext db;
public TeamForm()
{
InitializeComponent();
db = new SoccerContext();
db.Teams.Load();
dataGridView1.DataSource = db.Teams.Local.ToBindingList();
}
private void button1_Click(object sender, EventArgs e)
{
PTeam tmForm = new PTeam();
DialogResult result = tmForm.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
Team team = new Team();
team.Name = tmForm.textBox1.Text;
team.Coach = tmForm.textBox2.Text;
db.Teams.Add(team);
db.SaveChanges();
MessageBox.Show("Yangi obyekt qo’shildi");
}
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int index = dataGridView1.SelectedRows[0].Index;
int id = 0;
bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
if (converted == false)
return;
Team team = db.Teams.Find(id);
listBox1.DataSource = team.Players.ToList();
listBox1.DisplayMember = "Name";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int index = dataGridView1.SelectedRows[0].Index;
int id = 0;
bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
if (converted == false)
return;
Team team = db.Teams.Find(id);
team.Players.Clear();
db.Teams.Remove(team);
db.SaveChanges();
MessageBox.Show("Obyekt o’chirildi");
}
}
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int index = dataGridView1.SelectedRows[0].Index;
int id = 0;
bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
if (converted == false)
return;
Team team = db.Teams.Find(id);
PTeam tmForm = new PTeam();
tmForm.textBox1.Text = team.Name;
tmForm.textBox2.Text = team.Coach;
DialogResult result = tmForm.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
team.Name = tmForm.textBox1.Text;
team.Coach = tmForm.textBox2.Text;
db.Entry(team).State = EntityState.Modified;
db.SaveChanges();
MessageBox.Show("Obyekt yangilandi");
}
}
}
}
Ushbu kodga nimaga ahamiyat berish lozim? Birinchidan, lazy loading orqali jamoa va undagi futbolchilar kabi bog‘langan ma’lumotlarga ega bo’lamiz. Ularni ListBox: listBox1.DataSource = team.Players.ToList(); orqali yuklab olish mumkin.
Ikkinchidan, o’chirish jarayonida ushbu ro'yxatni team.Players.Clear(); kabi tozalashimiz mumkin.
Agar biz «qo’lda» DBni hosil qilgan bo’lsak, entity framework orqali database first yoki code first yondashuvi orqali loyihani uni biriktirishimiz mumkin. Tashqi kalitni hosil qilish jarayonida DBda kaskadli o’chirishni amalga oshirishimiz mumkin. Yoki jamoa o’chirilganda u bilan bog‘liq bo’lgan o’yinchilardagi maydon qiymatini null ga o’zgartirishimiz mumkin.
Ko’pga-ko’p aloqa
Ob’ektlar o’zaro ko’pga-ko’p aloqada bo’lishi mumkin. Bizda futbolchi va jamoa modeli mavjud bo’lsin. Futbolchi yillar davomida bir qancha jamoalarda to’p surishi mumkin. Bitta jamoada esa bir qancha futbolchilar to’p surishlari mumkin. Ushbu modellar quyidagicha tashkil qilinishi mumkin:
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection Players { get; set; }
public Team()
{
Players = new List();
}
}
public class Player
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public int Age { get; set; }
public ICollection Teams { get; set; }
public Player()
{
Teams = new List();
}
}
Ikkala model ham kolleksiya xususiyatidan iborat bo’lib, ushbu xususiyatlar orqali ko’pga-ko’p aloqa shakllantirilgan. Agar biz CodeFirst yondashuvidan foydalansak, DBda avtomatik tarzda quyidagi sxema shakllantiriladi:
Ya’ni ikkita ob’ektni o’zaro bog‘lovchi Player-Team jadval shakllantiriladi. Agar Player-Team yondashuvidan foydalansak, biz mustaqil tarzda ushbu jadvalni shakllantirishimiz lozim.
|