<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET 3.5 Extensions Preview</title><link>http://forums.asp.net/1147.aspx</link><description>Setup issues and general questions regarding this release. For specific topics use &lt;a href="http://forums.asp.net/1146.aspx"&gt;ASP.NET MVC&lt;/a&gt;, &lt;a href="http://forums.asp.net/1154.aspx"&gt;ASP.NET Controls for Silverlight&lt;/a&gt;, &lt;a href="http://forums.asp.net/1145.aspx"&gt;ASP.NET Dynamic Data&lt;/a&gt;, &lt;a href="http://go.microsoft.com/fwlink/?linkid=96773"&gt;ADO.NET Entity Framework&lt;/a&gt;, &lt;a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1430&amp;SiteID=1"&gt;ADO.NET Data Services&lt;/a&gt; or the &lt;a href="http://forums.asp.net/1008.aspx"&gt;ASP.NET AJAX UI&lt;/a&gt; forum.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: Entity Framework - Detached objects</title><link>http://forums.asp.net/thread/3172175.aspx</link><pubDate>Tue, 19 May 2009 09:20:57 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3172175</guid><dc:creator>cgeers</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3172175.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1147&amp;PostID=3172175</wfw:commentRss><description>&lt;p&gt;ruben0,&lt;/p&gt;&lt;p&gt;Using a single instance for your context (= singleton) is not the best approach for an ASP.NET application because as you&amp;#39;ve said each user will wind up using that instance. Using a singleton context would work better in a WinForms application.&lt;/p&gt;&lt;p&gt;Instantiating a new object for each &amp;quot;controller&amp;quot; class is a workable approach, but it not necessary either. A better approach would be to use a context per Http Request.&lt;/p&gt;&lt;p&gt;I wrote a blog post about this a while back:&lt;/p&gt;&lt;p&gt;&lt;a href="http://cgeers.wordpress.com/2009/02/21/entity-framework-objectcontext/" target="_blank"&gt;http://cgeers.wordpress.com/2009/02/21/entity-framework-objectcontext/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;You could then pass in this context instance to the constructor of your controller classes. You could opt to use dependency injection for this...&lt;/p&gt;&lt;p&gt;The following code is provided for illustrative purposes:&lt;br /&gt;&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;b id="1"&gt;1    &lt;/b&gt;&lt;span class="kwd"&gt;public class&lt;/span&gt; MyController: IController
&lt;b id="2"&gt;2    &lt;/b&gt;{
&lt;b id="3"&gt;3    &lt;/b&gt;  &lt;span class="kwd"&gt;private&lt;/span&gt; IObjectContextProvider provider;
&lt;b id="4"&gt;4    &lt;/b&gt;
&lt;b id="5"&gt;5    &lt;/b&gt;  &lt;span class="kwd"&gt;public&lt;/span&gt; MyController(IObjectContextProvider provider)
&lt;b id="6"&gt;6    &lt;/b&gt;  {
&lt;b id="7"&gt;7    &lt;/b&gt;    &lt;span class="kwd"&gt;this&lt;/span&gt;.provider = provider;
&lt;b id="8"&gt;8    &lt;/b&gt;  }
&lt;b id="9"&gt;9    &lt;/b&gt;
&lt;b id="10"&gt;10   &lt;/b&gt;  &lt;span class="kwd"&gt;public void&lt;/span&gt; SomeMethod()
&lt;b id="11"&gt;11   &lt;/b&gt;  {
&lt;b id="12"&gt;12   &lt;/b&gt;    MyContext context = provider.GetContext();
&lt;b id="13"&gt;13   &lt;/b&gt;    &lt;span class="cmt"&gt;// ...&lt;/span&gt;
&lt;b id="14"&gt;14   &lt;/b&gt;  }
&lt;b id="15"&gt;15   &lt;/b&gt;}
&lt;/pre&gt;&lt;pre class="coloredcode"&gt;Regards,&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;Christophe &lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: Entity Framework - Detached objects</title><link>http://forums.asp.net/thread/3106745.aspx</link><pubDate>Tue, 21 Apr 2009 07:13:09 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3106745</guid><dc:creator>MisterFantastic</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3106745.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1147&amp;PostID=3106745</wfw:commentRss><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;You can use a single context itself. Noneed of using two context for two controller. That will solve the problem.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Please let me know if you have problems. &lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Thani&lt;/p&gt;</description></item><item><title>Re: Entity Framework - Detached objects</title><link>http://forums.asp.net/thread/3032972.aspx</link><pubDate>Tue, 24 Mar 2009 09:35:36 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3032972</guid><dc:creator>puruforum</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3032972.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1147&amp;PostID=3032972</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;Hi,&lt;/p&gt;&lt;p&gt;Please explain more about it.&lt;/p&gt;&lt;p&gt;if you are using singleton and doing check with user ==null then another part of the code needed otherwise make user to null after savechanges.&lt;/p&gt;&lt;p&gt;and other solution is at the time of creation every time create new user. &lt;br /&gt;&lt;/p&gt;&lt;p&gt;Thanks,&lt;/p&gt;&lt;p&gt;Puru &lt;br /&gt;&lt;/p&gt;</description></item><item><title>Entity Framework - Detached objects</title><link>http://forums.asp.net/thread/3020567.aspx</link><pubDate>Thu, 19 Mar 2009 15:19:18 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3020567</guid><dc:creator>ruben0</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3020567.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1147&amp;PostID=3020567</wfw:commentRss><description>&lt;p&gt;First I was using a singleton class with only one instance of my entities (my context) but that means every user on the site uses that same instance, so I changed it to use a new instance per controller.&lt;/p&gt;&lt;p&gt;I&amp;#39;ve got my &amp;quot;UserController&amp;quot; and &amp;quot;CompanyController&amp;quot; (both used to update or get users/companies)&lt;/p&gt;&lt;p&gt;A User object has a link to Company.&lt;/p&gt;&lt;p&gt;What I do in my usercontroller:&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;b id="1"&gt;1    &lt;/b&gt;&lt;span class="kwd"&gt;public void&lt;/span&gt; addUser(UserDTO userDTO) {
&lt;b id="2"&gt;2    &lt;/b&gt;  User user = User.CreateUser(0, .....)
&lt;b id="3"&gt;3    &lt;/b&gt;  user.Company = &lt;span class="kwd"&gt;new&lt;/span&gt; CompanyController().getCompany(userDTO.Company.companyId);
&lt;b id="4"&gt;4    &lt;/b&gt;  context.AddToUsers(user);
&lt;b id="5"&gt;5    &lt;/b&gt;  context.SaveChanges();
&lt;b id="6"&gt;6    &lt;/b&gt;} 
&lt;/pre&gt;&lt;p&gt;&amp;nbsp;The problem I had here was the &amp;quot;Company&amp;quot; object was from another context (made in the companycontroller), so I thought, I&amp;#39;d just detach the Company object in CompanyController before I return it, so the &amp;quot;AddToUsers()&amp;quot; attaches it again.&lt;/p&gt;&lt;p&gt;Is there a way to make this &amp;quot;AddToUsers&amp;quot; attach this? I can solve my problem by using this code:&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;b id="1"&gt;1    &lt;/b&gt;Company c = &lt;span class="kwd"&gt;new&lt;/span&gt; Companycontroller().getCompany(userDTO.Company.companyId);
&lt;b id="2"&gt;2    &lt;/b&gt;context.AttachTo(&lt;span class="st"&gt;&amp;quot;Companies&amp;quot;&lt;/span&gt;, c);
&lt;b id="3"&gt;3    &lt;/b&gt;user.Company = c;
&lt;/pre&gt;&amp;nbsp; But I&amp;#39;d prefer a way it&amp;#39;s done automatically, because I have a lot more objects and having to hardcode all these attachments will give a lot of messy code..&lt;br /&gt;</description></item></channel></rss>