简体   繁体   English

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

[英]'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error

First of all I would like to mention that I have scoured the internet to find the solution to the problem however I couldn't get anything to work. 首先,我想提一下,我已经搜寻了互联网以找到解决问题的方法,但是我什么都没做。 I'm fairly new to EF so I don't have a complete understanding of DBContext or ObjectContexts so further explanation may be required. 我是EF的新手,因此我对DBContext或ObjectContext没有完全的了解,因此可能需要进一步的解释。 My EF code was built database first. 我的EF代码是首先建立数据库的。

My Scenario 我的场景

I have two tables in my data model: 我的数据模型中有两个表: 在此处输入图片说明

Below is my DriveDB class where I have my Client_Type attached (along with others that I have left out of the example for simplicity). 下面是我的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();
    }
}

My logic is that since the Client_Type tables is now attached all I have to do to update those tables is run the .SaveChanges() method and both tables will be updated. 我的逻辑是,由于现在附加了Client_Type表,因此我需要执行的所有更新操作都是运行.SaveChanges()方法,并且两个表都将被更新。 This is not the case however as apparently they don't share the same ObjectContext . 但事实并非如此,因为它们显然不共享相同的ObjectContext I realize there is a difference between DBContext and ObjectContext but I don't know how to overcome this problem. 我意识到DBContextObjectContext之间存在DBContext ,但是我不知道如何克服这个问题。

I have attempted to detach and attach Client_Type to DriveDB however I never found the correct solution. 我试图分离Client_Type并将其附加到DriveDB但是我从未找到正确的解决方案。

How can I update the database without the compiler throwing an exception? 如何在不引发编译器异常的情况下更新数据库?

Xaml: 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>

You have to update the state to every entry from every table as modified. 您必须将状态更新为修改后的每个表中的每个条目。 The tables are related, but they are still separate tables in your DbContext. 这些表是相关的,但是它们在您的DbContext中仍然是单独的表。 You can read more about your case here : 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 您可以在此处详细了解您的案件: http : //www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/updating-related-data-with NET MVC应用程序中的实体框架

So for those of you that are interested here is the solution I found. 因此,对于那些对此感兴趣的人,是我找到的解决方案。

My ItemSource for the ComboBox was bound to this property: 我的ComboBox ItemSource已绑定到此属性:

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

This made the ComboBox not only fail to read the SelectedItem value from the Record property but also throw the above exception when I tried to update the database with the new value. 这使得ComboBox不仅无法从Record属性读取SelectedItem值,而且在尝试使用新值更新数据库时也引发了上述异常。

I now have a property that only obtains the value from he database when the UserControl is loaded: 我现在有一个仅在加载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();
}

To reiterate... I believe the SelectedItem of the ComboBox must be set before the ItemSource 重申一下...我相信SelectedItem的的ComboBox必须在之前设置ItemSource

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - Entity Framework: “The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.” EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象Entity Framework - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework 无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 引发异常:因为两个对象附​​加到不同的ObjectContext对象,所以无法定义它们之间的关系 - Exception thrown: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架v4 - 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们已附加到不同的ObjectContext对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义关系,因为它们已附加到不同的ObjectContext对象 - Relationship cannot be defined because they are attached to different ObjectContext objects 无法找出哪些两个对象附​​加到不同的ObjectContext对象 - Cannot figure out which two objects are attached to different ObjectContext objects 无法定义两个对象之间的关系-为什么? - The relationship between the two objects cannot be defined - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM