【问题标题】:'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - 错误
【发布时间】:2015-10-12 10:23:48
【问题描述】:

首先我想提一下,我已经搜索了互联网以找到问题的解决方案,但是我无法解决任何问题。我对 EF 还很陌生,所以我对 DBContext 或 ObjectContexts 没有完全了解,因此可能需要进一步解释。我的 EF 代码是先建数据库的。

我的情景

我的数据模型中有两个表:

下面是我的 DriveDB 类,我在其中附加了我的 Client_Type(以及为简单起见我在示例中省略的其他类)。

public partial class DriveDB : DbContext
{
    public DriveDB()
        : base("name=DriveDB")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Client_Type> Client_Type { get; set; }
    public virtual DbSet<Entity> Entities { get; set; }
    public virtual DbSet<EntityContact> EntityContacts { get; set; }
    public virtual DbSet<Person> People { get; set; }
}

public class DataAccessService : IDataAccessService
{
    DriveDB MainRepository;
    public DataAccessService()
    {
        MainRepository = new DriveDB();
    }
    public void CommitChanges()
    {
        MainRepository.SaveChanges();
    }
}

我的逻辑是,由于现在附加了 Client_Type 表,因此我要更新这些表所需要做的就是运行 .SaveChanges() 方法,这两个表都将被更新。但情况并非如此,因为显然它们不共享相同的ObjectContext。我意识到DBContextObjectContext 之间存在差异,但我不知道如何解决这个问题。

我尝试将Client_Type 分离并附加到DriveDB,但我从未找到正确的解决方案。

如何在编译器不抛出异常的情况下更新数据库?

Xaml:

<ComboBox Style="{StaticResource DataComboBox}" ItemsSource="{Binding Client_type_list}" SelectedItem="{Binding Record.Client_Type, Mode=TwoWay}" ItemTemplate="{StaticResource Client_typeDataTemplate}"/>

<DataTemplate x:Key="Client_typeDataTemplate">
            <TextBlock Text="{Binding client_type1}"/>
        </DataTemplate>

【问题讨论】:

  • 您能否发布更新引发异常的实体的代码?调用 CommitChanges 之前的 IE 代码
  • “MainRepository”中记录的属性绑定到 WPF 控件,例如文本框,或者在出现此错误的情况下,它绑定到 ComboBox 请参阅 asnwer 中的 Xaml
  • Record 对象是一个实体

标签: c# entity-framework mvvm


【解决方案1】:

您必须将状态更新为已修改的每个表中的每个条目。这些表是相关的,但它们仍然是 DbContext 中的单独表。你可以在这里阅读更多关于你的案例:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

【讨论】:

    【解决方案2】:

    所以对于那些对此感兴趣的人,我找到了解决方案。

    ComboBoxItemSource 已绑定到此属性:

    public List<Client_Type> Client_type_list
    {
       get { return ServerPxy.GetClientTypes(); }
    }
    

    这使得ComboBox 不仅无法从Record 属性中读取SelectedItem 值,而且在我尝试使用新值更新数据库时抛出上述异常。

    我现在有一个属性,它只在加载 UserControl 时从他的数据库中获取值:

    private List<Client_Type> client_type_list;
    public List<Client_Type> Client_type_list
    {
        get { return client_type_list; }
        set
        {
            client_type_list = value;
            RaisePropertyChanged("Client_type_list");
        }
    }
    
    public void LoadExecute()
    {
        Client_type_list = ServerPxy.GetClientTypes();
    }
    

    重申...我认为ComboBoxSelectedItem必须设置在ItemSource之前

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多