locked
my navigation property cleared when close program RRS feed

  • Question

  • I use EF5 for data layer and I use Model First for create entity and relation between those.

    I have 2 entity Store,Stuff.

    these class have one to many association relationship with Stuffs and Store navigation fields.

    when I save for example 12 stuffs in store1.Stuffs with AddStuffToStore function everything is Ok and save successfully and i can load all stuffs with GroupStuffBy function but when restart program store1.stuffs.count=0 and i cant load those with GroupStuffBy.

    Store class:

    	
    
    I use EF5 for data layer and I use Model First for create entity and relation between those.
    
    I have 2 entity Store,Stuff.
    
    these class have one to many association relationship with Stuffs and Store navigation fields.
    
    when I save for example 12 stuffs in store1.Stuffs with AddStuffToStore function everything is Ok and save successfully and i can load all stuffs with GroupStuffBy function but when restart program store1.stuffs.count=0 and i cant load those with GroupStuffBy.
    
    Store class:
    
       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 Store()
        {
    
        }
    
    
        public Store(string title,string address,string tel,int state)
        {
    
    
            this.Title = title;
    
    
    
            this.Address = address;
    
            this.Tel = tel;
    
            this.State = state;
        }
    
    
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
                return false;
    
            Store store = obj as Store;
    
            return (this.Title == store.Title && this.Address==store.Address);
        }
    
    
        public void Save()
        {
    
    
            using (var storekeeper = new StoreKeeperContext())
            {
                bool flag = false;
                foreach (var item in storekeeper.Stores)
                {
                    if (item.Equals(this))
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    MessageBox.Show("مشخصات این کالا قبلا ثبت شده است", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
    
    
    
                try
                {
                    storekeeper.Stores.Add(this);
                    storekeeper.SaveChanges();
                }
                catch (Exception )
                {
                    MessageBox.Show( "عملیات ذخیره سازی با خطایی مواجه شد.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                }
    
    
    
    
            }
    
    
    
        }
    
    
        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 override string ToString()
        {
            return this.Title;
        }
    }

    Stuff class:

    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; }
        }
    
        private Factor factor;
    
        public Factor Factor
        {
            get { return factor; }
            set { factor = value; }
    }
        

    sorry for my english

    Monday, March 18, 2013 10:33 AM

All replies

  • Hi Behzad123,

    Welcome to the MSDN forum.

    I am not sure how you initialize the Store. Could you please let me know you how to initialize the Store object. I also don’t think define AddStuffToStore method, GroupStuffBy method and Save method in class Store is a good idea. It seems the class Store can do too much things.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 19, 2013 9:29 AM