akobar
یک شنبه 27 اسفند 1391, 21:28 عصر
سلام
بنده از EF 5 برای لایه دیتا استفاده کرده ام و با استفاده از model first موجودیت ها و روابط میان آنها را طراحی کرده ام.
دو کلاس دارم بصورت زیر که رابطه یک به چند با هم دارند:
public partial class Store
{
private int code;
[Key]
public int Code
{
get { return code; }
set { code = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
private string tel;
public string Tel
{
get { return tel; }
set { tel = value; }
}
private int state;//1 is deactive,0 is active
public int State
{
get { return state; }
set { state = value; }
}
private Storage storage;
public Storage Storage
{
get { return storage; }
set { storage = value; }
}
private ICollection <Stuff> stuffs = new List<Stuff>();
public ICollection<Stuff> Stuffs
{
get { return stuffs; }
set { stuffs = value; }
}
public void AddStuffToStore(Stuff stuff,int count)
{
for (int i = 0; i < count; i++)
{
Stuff s = stuff.Copy();
s.Store = this;
this.Stuffs.Add(s);
}
using (var storekeeper = new StoreKeeperContext())
{
try
{
storekeeper.SaveChanges();
}
catch (Exception)
{
MessageBox.Show("عملیات ذخیره سازی با خطایی مواجه شد.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public Dictionary<string,int> GroupStuffBy()
{
var result = (from t in this.Stuffs
group t by t.CompanyName into g
select new { stuffNmae = g.Key, stuffCount = g.Count() }).ToDictionary(h => h.stuffNmae, h => h.stuffCount);
return result;
}
}
public partial class Stuff
{
private int code;
[Key]
public int Code
{
get { return code; }
set { code = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string companyName;
public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}
private double cost;
public double Cost
{
get { return cost; }
set { cost = value; }
}
private bool selled;
public bool Selled
{
get { return selled; }
set { selled = value; }
}
private Store store;
internal Store Store
{
get { return store; }
set { store = value; }
}
}
برای ذخیره Stuff از تابع AddStuffToStore و برای خواندن از GroupStuffBy استفاده کرده ام
وقتی مثلا ۱۲ تا از stuff1 در انبار store1 ذخیره میکنم و برنامه بسته نشده درست ذخیره شده است اما وقتی دوباره برنامه را اجرا میکنم هیج محصولی در stuff1 نیست.
بنده از EF 5 برای لایه دیتا استفاده کرده ام و با استفاده از model first موجودیت ها و روابط میان آنها را طراحی کرده ام.
دو کلاس دارم بصورت زیر که رابطه یک به چند با هم دارند:
public partial class Store
{
private int code;
[Key]
public int Code
{
get { return code; }
set { code = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
private string tel;
public string Tel
{
get { return tel; }
set { tel = value; }
}
private int state;//1 is deactive,0 is active
public int State
{
get { return state; }
set { state = value; }
}
private Storage storage;
public Storage Storage
{
get { return storage; }
set { storage = value; }
}
private ICollection <Stuff> stuffs = new List<Stuff>();
public ICollection<Stuff> Stuffs
{
get { return stuffs; }
set { stuffs = value; }
}
public void AddStuffToStore(Stuff stuff,int count)
{
for (int i = 0; i < count; i++)
{
Stuff s = stuff.Copy();
s.Store = this;
this.Stuffs.Add(s);
}
using (var storekeeper = new StoreKeeperContext())
{
try
{
storekeeper.SaveChanges();
}
catch (Exception)
{
MessageBox.Show("عملیات ذخیره سازی با خطایی مواجه شد.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public Dictionary<string,int> GroupStuffBy()
{
var result = (from t in this.Stuffs
group t by t.CompanyName into g
select new { stuffNmae = g.Key, stuffCount = g.Count() }).ToDictionary(h => h.stuffNmae, h => h.stuffCount);
return result;
}
}
public partial class Stuff
{
private int code;
[Key]
public int Code
{
get { return code; }
set { code = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string companyName;
public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}
private double cost;
public double Cost
{
get { return cost; }
set { cost = value; }
}
private bool selled;
public bool Selled
{
get { return selled; }
set { selled = value; }
}
private Store store;
internal Store Store
{
get { return store; }
set { store = value; }
}
}
برای ذخیره Stuff از تابع AddStuffToStore و برای خواندن از GroupStuffBy استفاده کرده ام
وقتی مثلا ۱۲ تا از stuff1 در انبار store1 ذخیره میکنم و برنامه بسته نشده درست ذخیره شده است اما وقتی دوباره برنامه را اجرا میکنم هیج محصولی در stuff1 نیست.