使用Entity Framework保存组合框值时出现问题

时间:2010-10-28 15:00:04

标签: c# winforms entity-framework combobox

这个问题很简单,但我不明白为什么代码不起作用?

以下是我在表单上设置ComboBox成员的方法:

private void LoadUsersToComboBox()
{
    ScansEntities3 db = new ScansEntities3();

    comboBox1.DataSource = db.People;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "ID";
}

以下是我使用所选成员的方式:

private void CreateNewDepartment()
{
    if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
    {
        ScansEntities3 db = new ScansEntities3();  
        Department department = new Department()
        {
            Name = textBox1.Text,                    
            Person = (Person)comboBox1.SelectedItem,             
        };
        db.AddToDepartments(department);
        db.SaveChanges();
    }            
}

我收到了这个错误:

System.InvalidOperationException was unhandled
  Message=An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.EntityObject.System.Data.Objects.DataClasses.IEntityWithChangeTracker.SetChangeTracker(IEntityChangeTracker changeTracker)
       at System.Data.EntityUtil.SetChangeTrackerOntoEntity(Object entity, IEntityChangeTracker tracker)
       at System.Data.Objects.ObjectStateManager.AddEntry(Object dataObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
       at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, Object entity, String argumentName)
       at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
       at SQLite_Testing_Grounds.ScansEntities3.AddToDepartments(Department department) in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Model2.Designer.cs:line 89
       at SQLite_Testing_Grounds.Form1.CreateNewDepartment() in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Form1.cs:line 64
       at SQLite_Testing_Grounds.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Form1.cs:line 51
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SQLite_Testing_Grounds.Program.Main() in C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\SQLite Testing Grounds\SQLite Testing Grounds\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

还有一个问题可能导致此代码失败:

'Name'属性是一个字符串,因为它就是数据库中的字符串。 'Person'属性是Department.Person类型,在数据库中它是外键整数。

所以这不起作用:

    private void CreateNewDepartment()
    {
        if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
        {
            ScansEntities3 db = new ScansEntities3();  
            Department department = new Department()
            {
                Name = textBox1.Text,                    
                Person = ((Person)comboBox1.SelectedItem).ID,             
            };
            db.AddToDepartments(department);
            db.SaveChanges();
        }            
    }

1 个答案:

答案 0 :(得分:0)

我认为你必须处理你的db。这里的问题是SelectedItem仍附加到上下文。您可以使用上下文的Detach()方法将人员与上下文分离,但我认为您的真正问题可能是您的代码应如下所示:

private void LoadUsersToComboBox()
{
    using (ScansEntities3 db = new ScansEntities3())
    {
        comboBox1.DataSource = db.People;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
    }
}

如果这不能解决问题,请尝试以下方法:

private void LoadUsersToComboBox()
{
    using (ScansEntities3 db = new ScansEntities3())
    {
        var people = db.People.ToList();

        foreach (var person in people)
            db.Detach(person);

        comboBox1.DataSource = people;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
    }
}