Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Tuesday, November 11, 2014

Entity Framework Power Tools

Found this cool entity framework power tool to do reverse engineering for creating code first entity framework data models from an existing database.

Install the Entity Framework Power Tools Beta 4 from here:
https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Once the tool is installed, you should be able to access the entity framework reverse engineer plugin from Visual Studio.

Right click on your solution in Visual Studio and you should see menu option for reverse engineer:
"Entity Framework" -> "Reverse Engineer Code First"

Create a class library for your data layer and click "Reverse Engineer Code First" menu option. Folowwing the instructions to create entity framework data models for an existing database.


Monday, August 8, 2011

Entity Framework Error: Resource not found for segment 'Property'

I was getting "Resource not found for segment 'Property'" error with the below code. I am loading some data from the database using .Net Entity Framework.


 private void LoadItem(int specId)
 {
       var query = DataContext.Items.Where( x => x.SpecId == specID );

       DataServiceQuery dsq = DataServiceQuery)query;

       dsq.BeginExecute(  ar =>
       {
             this.items = dsq.EndExecute( ar ).SingleOrDefault();
       },
       null );
       }
 }

The above code threw an exception when the data query returned no records. I was expecting SingleOrDefault() to return default value (which is NULL) if no matching records were found. But, instead I was getting "Resource not found for segment 'Property'" exception.

After doing some investigation I found out that the exception was due to the fact that I was using primary key field in my data service query.

       var query = DataContext.Items.Where( x => x.SpecId == specID );

Here the field SpecId was a primary key. In Entity Framework if your data service query is performed on a primary key, then SingleOrDefault() and FirstOrDefault() methods will throw an exception if no matching records were found.

To resolve this issue, I set the following property to TRUE on the data service DataContext. This line will suppress the "Resource Not Found" exception.

DataConext.IgnoreResourceNotFoundException = true;