Posts

Showing posts with the label EntityFramework

EF6 Cheatsheet

Image

EF: The database cannot be opened because it is version 782

The full error message for this is "The database cannot be opened because it is version 782. This server supports version 706 and earlier. A downgrade path is not supported" and started appearing for me when I upgraded to Visual Studio 2015 Community. Originally I created a database file in visual studio 2013.

<add name="ProductContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\ProductContext.mdf;Initial Catalog=ProductContext;Integrated Security=True" providerName="System.Data.SqlClient" />

<add name="ProductContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ProductContext.mdf;Initial Catalog=ProductContext;Integrated Security=True" providerName="System.Data.SqlClient" />

Notes
I did not have to delete the local database files and have to start over, thankfully it was a small change.

Resource
http://sqlserverbuilds.blogs…

Entity Framework 6: Pull in Child Entities with Async WebAPI 2.0

Just a quick piece of code to get all the children under a given parent. The solutions is pretty straight forward but took sometime to think about since I wanted to keep the result asynchronous. I was also thinking of how I would use scaffolds creates other apis with my models.

Code:

db.Product.Include(x=>x.ProductChildren).SingleOrDefaultAsync(x => x.Id == id);
Notes:
Its late at nite (so no judging...) but I wanted to get my thoughts down and explore this code for a bit. The piece code is async but my question is does it pull the child entities after it has filter down on the parent id? Or does it pulls in all the children before it has filtered down on a particular partent id and if not wouldn't a more proper way to do it be more something like...

db.Product.FindAsync(id).Include(x=>x.ProductChildren);
This code doesn't work of course, since the Include doesn't exist for 'FindAsync' but either way alternative I believe might be with the 'ContinueWi…

Using Entity Framework and a Storage Procedure with Parameters

So I was stuck sometime ago on a problem I had where I was using my sql store procedure with the Entity Framework. Eventually found what I needed and the purpose of this article is just to write out a small example. I've come up with multiple ways to achieve the same thing now but this is the most basic form of it.

public IList<KeyValuePair> GetIds(string id) { using (var db = DBFactory.GetDB()) { return db.Database.SqlQuery<KeyValuePair>( "EXEC db.MyDatabase @id", new SqlParameter("id", id)); } }
Resources How to pass parameters to DbSet.SqlQuery Method in Entity Framework How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5 The data reader has more than one field. Multiple fields are not valid for EDM primitive types
KeyValuePair(TKey, TValue) Structure (System.Collections.Generic)