将额外的 ObjectSet 从单独的项目附加到 ObjectContext
我希望这是有道理的。我有一个使用实体框架的 ASP.NET Web 应用程序。我已向数据库添加了几个自定义表,并创建了一个单独的项目来处理这些表的 CRUD 操作。我选择了单独的项目,因为我不希望将来的应用程序升级覆盖我的自定义功能。
我的问题是这样的。如何将自定义 ObjectContext 附加/组合到应用程序的 ObjectContext?我想使用相同的 UnitOfWorkScope(已在应用程序中)来维护每个 HTTP 请求的一个 ObjectContext 实例。同样,出于上面列出的原因,我不想将我的 ObjectSet 添加到应用程序的 ObjectContext 中。
下面是一些代码:
Widget.cs
public partial class Widget
{
public Widget()
{
}
public int WidgetId {get;set;}
public string WidgetName {get;set;}
}
WidgetObjectContext.cs
public partial class WidgetObjectContext : ObjectContext
{
private readonly Dictionary<Type, object> _entitySets;
public ObjectSet<T> EntitySet<T>()
where T : BaseEntity
{
var t = typeof(T);
object match;
if(!_entitySets.TryGetValue(t, out match))
{
match = CreateObjectSet<T>();
_entitySets.Add(t, match);
}
return (ObjectSet<T>)match;
}
public ObjectSet<Widget> Widgets
{
get
{
if((_widgets == null))
{
_widgets = CreateObjectSet<Widget>();
}
return _widget;
}
}
private ObjectSet<Widget> _widgets;
在我的 WidgetManager 类中,如果我使用应用程序的 ObjectContext,我会像这样查询我的表:
var context = ObjectContextHelper.CurrentObjectContext;
var query = from c in context.ObjectSet .... etc
我想要做的是这样的事情:
var context = ObjectContextHelper.CurrentObjectContext.Attach(WidgetObjectContext);
我知道这行不通,但是那是我想要实现的目标的要点。希望这足够清楚。谢谢。
I hope this makes sense. I have a ASP.NET web application that uses Entity Framework. I have added a couple of custom tables to the db and created a separate project to handle the CRUD operations for those tables. I chose the separate project because I don't want future upgrades to the application to overwrite my custom features.
My problem is this. How do I attach/combine my custom ObjectContext to the ObjectContext of the application? I want to use the same UnitOfWorkScope (already in the application) to maintain the one ObjectContext instance per HTTP request. Again, I don't want to add my ObjectSets to the application's ObjectContext for my reason listed above.
Here is some code:
Widget.cs
public partial class Widget
{
public Widget()
{
}
public int WidgetId {get;set;}
public string WidgetName {get;set;}
}
WidgetObjectContext.cs
public partial class WidgetObjectContext : ObjectContext
{
private readonly Dictionary<Type, object> _entitySets;
public ObjectSet<T> EntitySet<T>()
where T : BaseEntity
{
var t = typeof(T);
object match;
if(!_entitySets.TryGetValue(t, out match))
{
match = CreateObjectSet<T>();
_entitySets.Add(t, match);
}
return (ObjectSet<T>)match;
}
public ObjectSet<Widget> Widgets
{
get
{
if((_widgets == null))
{
_widgets = CreateObjectSet<Widget>();
}
return _widget;
}
}
private ObjectSet<Widget> _widgets;
In my WidgetManager class if I was using the application's ObjectContext I would query my tables like this:
var context = ObjectContextHelper.CurrentObjectContext;
var query = from c in context.ObjectSet .... etc
What I want would be to do something like this:
var context = ObjectContextHelper.CurrentObjectContext.Attach(WidgetObjectContext);
I know this won't work but that is the gist of what I am trying to accomplish. Hope this is clear enough. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是不可能的。 ObjectContext 创建连接到描述映射和数据库的元数据的实体连接。但是您必须使用不同的元数据集 - 一组用于 ASP.NET 应用程序,一组用于单独的项目。只需两个连接即可使用这些模型 =>您需要两个 ObjectContext。
I don't think it is possible. ObjectContext creates entity connection which connects to metadata describing mapping and database. But you have to different sets of metadata - one for ASP.NET application and one for separate project. Simply you need two connection to work with these models => you need two ObjectContexts.
仅供参考:之前的答案在回答时是正确的。现在可以使用 EF 4.1 中提供的 DbContext 来执行此操作。需要注意的是,您必须使用代码优先策略才能构建自定义上下文。换句话说,您将无法使用 EDMX 文件来完成此操作。
FYI: The previous answer was correct at the time of the answer. It is now possible to do this using the DbContext available in EF 4.1. The caveat is that you must use the code-first strategy in order to build your custom context. In other words, you won't be able to use EDMX files to accomplish this.