<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx</link><description>&amp;#160; The information in this post is out of date. Visit msdn.com/data/ef for the latest information on current and past releases of EF. For Finding Entities see http://msdn.com/data/jj573936 &amp;#160; Introduction Version 4.1 of the Entity Framework contains</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10335236</link><pubDate>Tue, 31 Jul 2012 16:40:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10335236</guid><dc:creator>Arthur Vickers - MSFT</dc:creator><description>&lt;p&gt;@Ben Hayat Find looks in your context first for an entity with the given key. If no entity is found in the context it then looks in the database. So if the entity is already being tracked by the context then it will be returned without hitting the database, but if it is not being tracked by the context and has been saved to the database then Find will hit the database and return the entity. This means that if you save an entity in one context then you can get it from the database with Find from a different context.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10335236" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10334645</link><pubDate>Mon, 30 Jul 2012 04:32:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10334645</guid><dc:creator>Ben Hayat</dc:creator><description>&lt;p&gt;Hi Arthur;&lt;/p&gt;
&lt;p&gt;Firstly, I like to thank you the way you had answered all the questions with useful and informative answers.&lt;/p&gt;
&lt;p&gt;My confusion with &amp;quot;Find&amp;quot; comes with the clause &amp;quot;Find will return entities that are in the Added state&amp;quot;. Does this mean with &amp;quot;Find&amp;quot; I can only look for an entity by primary ID, ONLY while my DbContext is alive and I can ONLY see entities that have been added while this DbContext instance was created? Another words, if I create another instance of DbContext and look for the same Entity, I will NOT find it, because it&amp;#39;s not hitting the database???&lt;/p&gt;
&lt;p&gt;This is my confusion about &amp;quot;Not hitting the database&amp;quot;.&lt;/p&gt;
&lt;p&gt;Thank you in advance.&lt;/p&gt;
&lt;p&gt;..Ben&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10334645" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10330294</link><pubDate>Mon, 16 Jul 2012 20:03:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330294</guid><dc:creator>Arthur Vickers - MSFT</dc:creator><description>&lt;p&gt;@Thupten Find does not create a LINQ query and so does not support these methods. If you are writing a LINQ query (such as is shown in &amp;quot;Finding entities using a query&amp;quot;) then they work in the normal way--for more details see the MSDN documentation for the methods or any of the many other resources that describe LINQ.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330294" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10330285</link><pubDate>Mon, 16 Jul 2012 19:46:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330285</guid><dc:creator>Thupten</dc:creator><description>&lt;p&gt;no explanation about how to use Any(), Select(), All() methods?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330285" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10321452</link><pubDate>Mon, 18 Jun 2012 18:44:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321452</guid><dc:creator>Arthur Vickers - MSFT</dc:creator><description>&lt;p&gt;@Jules.Bartow Find doesn&amp;#39;t currently support eager loading. It is something we have heard requests for and may add in the future. Find is mostly for looking up a single entity by primary key without hitting the database if that entity is already loaded into the context. This is an unambiguous operation. Once you start to extend this concept to query by things other than the primary key or to include more than one entity it starts to get a bit more difficult to define when to hit the database, which part of the query should/can be done locally, and how to deal with semantic differences between local and database queries. This is not to say we couldn&amp;#39;t define something that would work and be useful, but it is non-trivial, and the benefits of doing this with Find over just using LINQ to Entities are not always obvious. So, for the moment, if you want more than just a single entity looked up by primary key then the way to do it is with LINQ.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321452" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10321446</link><pubDate>Mon, 18 Jun 2012 18:32:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321446</guid><dc:creator>Jules.Bartow</dc:creator><description>&lt;p&gt;Arthur, thank you for letting me know the EF connection string is only used to provide references to the XML containing the model. &amp;nbsp;Now that I&amp;#39;m past that hump I&amp;#39;m eager to learn why .Find doesn&amp;#39;t work when I&amp;#39;m trying to eager load...&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Users usr = db.Users.Find(UserID); &amp;nbsp;// works&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Users usr = db.Users.Include(Images).SingleOrDefault(f =&amp;gt; f.UserID == model.UserID); &amp;nbsp;//works&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Users usr = db.Users.Include(Images).Find(UserID); &amp;nbsp; //FAILS&lt;/p&gt;
&lt;p&gt;Am I retarded (EF will Eager Load from my database automagically???)) or is .Find retarded and doesn&amp;#39;t let you do eager loading?&lt;/p&gt;
&lt;p&gt;&amp;#39;System.Linq.IQueryable&amp;lt;MVCApp3.Models.Users&amp;gt;&amp;#39; does not contain a definition for &amp;#39;Find&amp;#39; and no extension method &amp;#39;Find&amp;#39; accepting a first argument of type &amp;#39;System.Linq.IQueryable&amp;lt;MVCApp3.Models.Users&amp;gt;&amp;#39; could be found (are you missing a using directive or an assembly reference?)	C:\Users\Jb\Documents\Visual Studio 11\Projects\MVCApp3\MVCApp3\Controllers\IdentityController.cs&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321446" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10312422</link><pubDate>Thu, 31 May 2012 15:01:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10312422</guid><dc:creator>Arthur Vickers - MSFT</dc:creator><description>&lt;p&gt;@Jules.Bartow To use Code First you need to use a &amp;quot;normal&amp;quot; connection string rather than an EF connection string. This is because the EF connection string is only used to provide references to the XML containing the model which is not needed in Code First since the model is defined by the code. Try something like this:&lt;/p&gt;
&lt;p&gt;&amp;lt;add name=&amp;quot;UnicornsEntities&amp;quot; connectionString=&amp;quot;data source=jb-laptop\jblaptop;initial catalog=Unicorns;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot; providerName=&amp;quot;System.Data.SqlClient&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10312422" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10312121</link><pubDate>Thu, 31 May 2012 01:20:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10312121</guid><dc:creator>Jules.Bartow</dc:creator><description>&lt;p&gt;I&amp;#39;m using EF 4.3.1 and having difficulty getting your example code to work. &amp;nbsp;Don&amp;#39;t know if 4.3.1 is substantially different from 4.1 example or I&amp;#39;m doing something wrong. &amp;nbsp;Also tried this in EF5.0 pre-release --no joy either.&lt;/p&gt;
&lt;p&gt;var unicorns = from u in context.Unicorns&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where u.Name.StartsWith(&amp;quot;B&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; select u;&lt;/p&gt;
&lt;p&gt;produces error: MetadataException was unhandled&lt;/p&gt;
&lt;p&gt;At least one SSDL artifact is required for creating StoreItemCollection.&lt;/p&gt;
&lt;p&gt;Doesn&amp;#39;t your &amp;quot;Code First&amp;quot; build the CSDL, SSDL, and MSL automagically as part of the assembly? &amp;nbsp;If not, how, what, and where do I specify it? &amp;nbsp;I&amp;#39;m not using SQLCE or SQL Express, but regular SQL Server.&lt;/p&gt;
&lt;p&gt;Shouldn&amp;#39;t a Unicorns.MDF file be created somewhere with the Unicorns catalog added so SQL Server Management Studio sees it if it doesn&amp;#39;t exist? &amp;nbsp; Can you post a working solution? &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class UnicornsContext : System.Data.Entity.DbContext&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public UnicornsContext() : base(&amp;quot;UnicornsEntities&amp;quot;) { }&lt;/p&gt;
&lt;p&gt;My App.Config file:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;configuration&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;configSections&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;!-- For more information on Entity Framework configuration, visit &lt;a rel="nofollow" target="_new" href="http://go.microsoft.com/fwlink/?LinkID=237468"&gt;go.microsoft.com/fwlink&lt;/a&gt; --&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;section name=&amp;quot;entityFramework&amp;quot; type=&amp;quot;System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;/configSections&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;connectionStrings&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;add name=&amp;quot;UnicornsEntities&amp;quot; connectionString=&amp;quot;Metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=jb-laptop\jblaptop;initial catalog=Unicorns;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;&amp;quot; providerName=&amp;quot;System.Data.EntityClient&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;/connectionStrings&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;entityFramework&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;defaultConnectionFactory type=&amp;quot;System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;parameters&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;parameter value=&amp;quot;Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/parameters&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;/defaultConnectionFactory&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;/entityFramework&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/configuration&amp;gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10312121" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10260310</link><pubDate>Tue, 24 Jan 2012 22:51:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10260310</guid><dc:creator>Arthur</dc:creator><description>&lt;p&gt;@Sumesh: The code you posted should work. I’m not sure why it’s not working for you without more details.&lt;/p&gt;
&lt;p&gt;@pk: Most of the time it is not necessary to use the Create method to create entities. If you are using change tracking proxies, then you may need to do this to ensure that your get a change tracking proxy, but none of these examples use change tracking proxies. When using lazy loading proxies it usually doesn’t matter that you don’t have a proxy for the new entity because you usually don’t expect lazy loading for this entity anyway, since it isn’t even in the database yet.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Arthur&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10260310" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 3: Finding Entities</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/28/using-dbcontext-in-ef-feature-ctp5-part-3-finding-entities.aspx#10173993</link><pubDate>Mon, 13 Jun 2011 17:06:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173993</guid><dc:creator>pk</dc:creator><description>&lt;p&gt;Everything I&amp;#39;ve read about EF 4.1 suggests using something like new Unicorn { Id = -1 } is not good practice. It should be: context.Unicorns.Create(). I know this is a minor point, and not really critical to the current topic, but it would be nice if the ADO.NET developer&amp;#39;s posts were a bit more consistent, to help users avoid these little gotchas.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173993" width="1" height="1"&gt;</description></item></channel></rss>