实体框架不同的对象上下文:使用来自usercontrols的实体 [英] Entity Framework Different Object Context: Using entities from usercontrols

查看:63
本文介绍了实体框架不同的对象上下文:使用来自usercontrols的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是新来的论坛,需要一些帮助。我最近在上个月开始使用EF,并发现它到目前为止非常有用。

但是,我需要帮助理解'ObjectContext',特别是我遇到的问题。 />我有一个带有AJAX用户控件的ASPX页面。

在我的EF中我有'Project'对象,有'User'对象列表和'Role'对象因此,在使用linq查询的主Page_Load上,我加载Project对象,并从中选择一个用户列表到AJAX用户控件的属性。

例如:

Project.Users.Load(); //加载用户
AjaxUserControl.UserList =(来自Project.Users中的U,其中U.ID> 200选择U).Tolist(); ///选择项目中的用户。

AJAX用户控件是一个带有"添加"和"删除"功能的数据绑定列表。我将linq查询输入为List< T>所以我可以。添加和。从用户控件中删除它中的对象。

所以在UserControl中,如果我选择Add,代码将执行以下操作:

Model.User User = new Model.User();
User.Firstname = TextBox.Text;
List.Add(User);

列表保存到和来自一个会话,以便用户控制跟踪列表。

无论如何,当我在主页面上选择保存时,主代码将从用户控件中检索当前列表,并尝试将用户添加到和删除从这样的原始项目对象:

列表< Model.User> Users = AjaxUserControl.UserList;

foreach(用户中的Model.User用户)
{
if if(User.EntityState == EntityState.Added)
{{br /> Project.Users.Add(User)
}
}


除了,我一直得到错误:
无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象

我甚至尝试过提供原始对象对于AJAXUserControl中的属性的ObjectContext,所以当我添加一个用户时,可以从相同的模型中选择它。

对于这个问题似乎很少解释,有人可以帮助我吗?

谢谢,

劳伦斯

有人可以帮我解决这个问题


Laz

Hello all,

I am new to the forum, and need a little bit of help with something.  I have recently in the last month started using the EF, and found it quite useful so far.

However, I need a little help understanding 'ObjectContext', especially with a problem I am encountering.

I have a ASPX page with an AJAX usercontrol within it.

In my EF i have 'Project' object, with a list of 'User' objects, and 'Role' objects etc.  So, on the main Page_Load using a linq query, I load the Project object, and select a list of users from it into a property on the AJAX user control.


For example: 

Project.Users.Load();  //Loads the users
AjaxUserControl.UserList = (from U in Project.Users where U.ID>200 select U).Tolist();   ///Selects the users in the project.

The AJAX usercontrol is a databound list with 'Add' and 'Remove' functionality.  I input the linq query as a List<T> so that i can .Add and .Remove objects from it in the usercontrol.

So in the UserControl, if i choose Add, the code will do something like this:

Model.User User = new Model.User();
User.Firstname = TextBox.Text;
List.Add(User);

The List is saved to and from a session so that the user control tracks the list.

Anyway, when I choose Save on the main page, the main code will retreive the current list from the usercontrol, and try adding the users to and removing from the original proejct object like this:

List<Model.User> Users = AjaxUserControl.UserList;

foreach(Model.User User in Users)
{
if(User.EntityState == EntityState.Added)
   {
    Project.Users.Add(User)
   }
}





HOWEVER, I keep getting the error that:

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

I have even tried supplying the original ObjectContext to a property in the AJAXUserControl so when I Add a user, it can be selected from the suppoedly same model.

There seems to be very little explaining further into this problem, could someone assist me please?

Thanks,

Laurence

Can someone please help me out with this


Laz

推荐答案

Juut澄清一下,错误发生在Project.users.Add(User)行上。
Juut to clarify, the error occurs on the line Project.users.Add(User).


这篇关于实体框架不同的对象上下文:使用来自usercontrols的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆