none
EF Code First: An entity that belongs to multiple collections RRS feed

  • Question

  • Hello experts,

    I have a situation where the entity Channel belongs to the entity Unit. I have created a new entity ChannelView which should be linked to a single Channel. The problem is that when I call MyChannelView.Channel = MyUnit.Channels[0] for example, EF moves the Channel to the view. Instead, I would like the view to point to the Channel without removing the reference from the Unit. They are defined like so:

     

     

        public class Channel
        {
            public int ID { get; set; }        
            public string ChannelNumber { get; set; }
            public virtual Unit Unit { get; set; }
            public virtual ICollection<ChannelValue> ChannelValues { get; set; }
    
            public Channel()
            {
                ChannelValues = new List<ChannelValue>();
            }
        }
    
    
        public class Unit
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public ICollection<Channel> Channels { get; set; }
    
            public Unit()
            {
                Channels = new List<Channel>();
            }
        }
    
    
    
        public class ChannelView
        {
            public int ID { get; set; }
            public virtual Channel Channel { get; set; }
        }
    
    


     


    Friday, September 23, 2011 9:44 PM

Answers

  • After further investigation, the data is connected as far as the database goes. The problem was how I was printing it out in that the lazy loading did not include the Channels for Units of the Company. Sorry for the confusion.
    • Marked as answer by Thomas Kendall Monday, September 26, 2011 3:02 PM
    Monday, September 26, 2011 3:02 PM

All replies

  • It turns out that this is not what was happening. Apparently, the Channel was never associated with the Unit. What am I doing wrong in my initializer?

     

            protected override void Seed(RemoteMonitorDB context)
            {
                Debug.WriteLine("Seeding Database...");
    
                // Create a new user
                User admin = new User() { EmailAddress = "admin", PasswordHash = "password".GetHashCode(), RootView = new GroupViewNode() { Name = "Root" } };
                context.Users.Add(admin);
    
                // Create a new company
                Company company1 = new Company() { Name = "Company #1" };
                context.Companies.Add(company1);
    
                // Add the admin user to the company
                admin.Company = company1;
    
                // Create a unit and add it to the company
                Unit unit1 = new Unit() { Name = "Unit #1" };
                company1.Units.Add(unit1);
    
                // Add a channel
                Channel ch1 = new Channel() { ChannelNumber = "CH1" };
                unit1.Channels.Add(ch1);
    
                // Add some channel values
                ChannelValue cv1 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now, Value = "10.00" };
                ch1.ChannelValues.Add(cv1);
                ChannelValue cv2 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now.AddSeconds(10), Value = "9.00" };
                ch1.ChannelValues.Add(cv2);
                ChannelValue cv3 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now.AddSeconds(-10), Value = "11.00" };
                ch1.ChannelValues.Add(cv3);
                ChannelValue cv4 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now.AddSeconds(20), Value = "8.00" };
                ch1.ChannelValues.Add(cv4);
                ChannelValue cv5 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now.AddSeconds(-20), Value = "12.00" };
                ch1.ChannelValues.Add(cv5);
                ChannelValue cv6 = new ChannelValue() { Channel = ch1, TimeStamp = DateTime.Now.AddSeconds(30), Value = "7.00" };
                ch1.ChannelValues.Add(cv6);
    
                // Create a new Group for holding groups            
                GroupViewNode ntgNewUnits = new GroupViewNode() { Name = "New Units" };
                admin.RootView.Children.Add(ntgNewUnits);
    
                // Create a sub-group
                GroupViewNode vipView = new GroupViewNode() { Name = "VIP Group" };
                ntgNewUnits.Children.Add(vipView);
                //vipView.Children.Add(new ChannelViewNode() { Name = "VIP Channel 1 ViewNode", Channel = ch1 });
    
                // Create another sub-group
                GroupViewNode ntgSubGroup2 = new GroupViewNode() { Name = "Child Group 2" };
                ntgNewUnits.Children.Add(ntgSubGroup2);
    
                base.Seed(context);
            }
    
    


    Monday, September 26, 2011 2:56 PM
  • After further investigation, the data is connected as far as the database goes. The problem was how I was printing it out in that the lazy loading did not include the Channels for Units of the Company. Sorry for the confusion.
    • Marked as answer by Thomas Kendall Monday, September 26, 2011 3:02 PM
    Monday, September 26, 2011 3:02 PM