Note that there are some explanatory texts on larger screens.

plurals
  1. POAttach additional ObjectSets to ObjectContext from separate project
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Here is some code:</p> <p>Widget.cs</p> <pre><code>public partial class Widget { public Widget() { } public int WidgetId {get;set;} public string WidgetName {get;set;} } </code></pre> <p>WidgetObjectContext.cs</p> <pre><code>public partial class WidgetObjectContext : ObjectContext { private readonly Dictionary&lt;Type, object&gt; _entitySets; public ObjectSet&lt;T&gt; EntitySet&lt;T&gt;() where T : BaseEntity { var t = typeof(T); object match; if(!_entitySets.TryGetValue(t, out match)) { match = CreateObjectSet&lt;T&gt;(); _entitySets.Add(t, match); } return (ObjectSet&lt;T&gt;)match; } public ObjectSet&lt;Widget&gt; Widgets { get { if((_widgets == null)) { _widgets = CreateObjectSet&lt;Widget&gt;(); } return _widget; } } private ObjectSet&lt;Widget&gt; _widgets; </code></pre> <p>In my WidgetManager class if I was using the application's ObjectContext I would query my tables like this:</p> <pre><code>var context = ObjectContextHelper.CurrentObjectContext; var query = from c in context.ObjectSet .... etc </code></pre> <p>What I want would be to do something like this:</p> <pre><code>var context = ObjectContextHelper.CurrentObjectContext.Attach(WidgetObjectContext); </code></pre> <p>I know this won't work but that is the gist of what I am trying to accomplish. Hope this is clear enough. Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload