与附加/分离混淆和不同的ObjectContext对象

时间:2012-11-10 20:39:40

标签: c# wpf entity-framework ado.net

我打开一个新的MessagesWindow,其中包含以下代码:

namespace Newsletter.UI
{
    public partial class MessagesWindow : Window
    {
        MessageService msgService;
        public MessagesWindow()
        {
            InitializeComponent();
            msgService = new MessageService();
        }

        private void CreateButtonClick(object sender, RoutedEventArgs e)
        {
            var createMessageWindow = new CreateMessageWindow { Owner = this };
            createMessageWindow.ShowDialog();
            gridMessages.ItemsSource = from msg in msgService.GetAllMessages()
                                       select msg;
        }

        private void messagesWindow_Loaded(object sender, RoutedEventArgs e)
        {          
            gridMessages.ItemsSource = from msg in msgService.GetAllMessages()
                                       select msg;
        }

        private void EditButtonClick(object sender, RoutedEventArgs e)
        {

            if (gridMessages.SelectedItem == null)
                MessageBox.Show("You have to choose the message first.");
            else
            {
                var selectedMessage = (Message)gridMessages.SelectedItem;

                var editMessageWindow = new EditMessageWindow(selectedMessage);
                editMessageWindow.Owner = this;
                editMessageWindow.ShowDialog(); 
            }
            gridMessages.ItemsSource = from msg in msgService.GetAllMessages()
                                        select msg;
        }
    }
}

我打开一个包含EditButtonClick的新窗口,我想要编辑MessageEditMessageWindow的代码:

namespace Newsletter.UI
{
    public partial class EditMessageWindow : Window
    {
        private Message editMessage;
        private MessageService messageService;
        public EditMessageWindow(Message message)
        {
            InitializeComponent();
            editMessage = message;
            messageService = new MessageService();

        }

        private void CreateMessageWindowLoaded(object sender, RoutedEventArgs e)
        {
            subject.Text = editMessage.Subject;
            messageContent.AppendText(editMessage.Content);
            senderComboBox.ItemsSource = messageService.GetAllSenders();

            mailingListComboBox.ItemsSource = messageService.GetAllMailingLists();

        }

        private void SaveClick(object sender, RoutedEventArgs e)
        {            
            editMessage.Date = DateTime.Now;
            editMessage.Content = messageContent.Text;
            editMessage.MailingList.Add(messageService.GetAllMailingLists()[mailingListComboBox.SelectedIndex]); //ERROR
            editMessage.Sender = messageService.GetAllSenders()[senderComboBox.SelectedIndex];
            editMessage.Subject = subject.Text;
            editMessage.HasAttachments = false;
            messageService.EditMessage(editMessage);

            Close();
        }
    }
}

我在SaveClick上收到错误:

  

无法定义两个对象之间的关系,因为   它们附加到不同的ObjectContext对象。

最后,MessageService代码:

namespace Newsletter.Services
{
    public class MessageService
    {
        private NewsletterEntities _context;

        public MessageService()
        {
            _context = new NewsletterEntities();
        }
        public void AddMessage(Message newMessage)
        {
            _context.Messages.AddObject(newMessage);
            _context.SaveChanges();
        }
        public void EditMessage(Message editMessage)
        {
            var messageToChange = (from m in _context.Messages where editMessage.MessageID == m.MessageID select m).FirstOrDefault();
            if (messageToChange != null)
            {
                messageToChange = editMessage;
                _context.SaveChanges();
            }
        }

        public List<Message> GetAllMessages()
        {
            return (from m in _context.Messages select m).ToList();
        } 

        public List<MailingList> GetAllMailingLists()
        {
            var query = from m in _context.MailingLists select m;
            return query.ToList();
        }

        public List<Sender> GetAllSenders()
        {
            var query = from m in _context.Senders select m;
            return query.ToList();
        }

        public void DeleteMessage(Message message)
        {

            List<MailingList> mailingLists = new List<MailingList>();
            foreach(MailingList ml in message.MailingList.ToList())
            {
                ml.Message.Remove(message);
            }
            _context.Messages.DeleteObject(message);
            _context.SaveChanges();
        }
    }
}

我正在被所有这些层和背景所迷惑。我知道我应该以某种方式分离信息,用它做一些事情并附上但我不知道在哪里和如何。我应该在MessageService,UI图层中的服务层,还是在我拥有所有实体的DAL图层中执行此操作?或者我应该重新设计我的解决方案并在不同的地方执行上下文逻辑?

1 个答案:

答案 0 :(得分:1)

您似乎每个窗口都设置了上下文。这将对您在上下文中维护对象的方式产生2个影响。

  1. 您不希望将对象从表单传递到表单。相反,您将从表单传递ID到表单并在上下文中加载实体。
  2. 当一个实体在一个上下文中更新时,您需要通知其他窗口/上下文,以便他们可以刷新数据。
  3. 这是一篇MSDN文章,详细介绍了每个表单的上下文过程。本文使用NHibernate,但EF和DbCotext

    的过程几乎完全相同

    http://msdn.microsoft.com/en-us/magazine/ee819139.aspx