Entity Frame Core And Microsoft Sql InvalidOperationException

Thought i would just share something i ran across that had me kinda scratching my head in frustration (all though that seems to happening a lot theses days 😦 )

When your setting up you EF Core to work with your stored procedures that you’ve created on your database when calling your store procedures using raw sql commands as shown in the image below .

blog1

In my Database Context class i have a DbSet that uses a model called “Game_Tbl”  but i was continually running in this exception when i was trying to query my stored procedure .

blogException

This was the stored procedure in question

blog5no_gID

The problem was in my model class .

blog4

The data i was requested from the SQL stored procedure i created was causing and error because of the gID property in my Game_Tbl Model class it couldnt map the data properly which was causing and InvalidOperationError.

The solution was simple i just had to add gID to the query to correspond to my models property  . Unfortunately none of the properties of the model class can be null or any exception will be thrown in EF Core FromSql method.

 

blog3

 

DBContext ,DBSet and Sql Server Relationship

It Explain it to me like im five!.

When using code first to create a table in an sql server the tables schema is design according to the dbSets classes being used the designer looks for a class that is inheriting DBContext .

The DBSets inside this class is kind of like a builder showing up to the office were he works and asking for the blueprints for the building his about to put up for the building.

So imagine each DBSet is a floor in that building each DBSet is a new floor with the specified amount of rooms according to the blueprint .

After its all lay’d out and built up you can now access each room and floor in the building using the DBSet . So using the methods inside of DBSet you can access the value of objects from the sql servers table kind of like going into a room and getting something except you can take all the items from the rooms and bring them with you and you only have to go back to the building if something changes in one of the rooms , saving you ALLLLLLL that walking back and forth aint that great!? 🙂 lol .

DBset Advantages of Using Find()

So apparently people used to always have to write  something like this when querying the db

context.People.SingleOrDefault(p => p.PersonId == _personId)

but using the find() method is much more efficient for single object query’s , it first checks if the item your look for is already being tracked in memory and saves you a query back to the server and if its not in memory then it will go to the database and get it .

Using find also will grab items from the context that have not been saved back to the Database yet .

Its order of opertation is summed up in the book ” Programming Entity FrameWork:DBcontext” heres the order of operation for Context.Find() from the
book.

Find uses a simple set of rules to locate the object (in order of precedence):
1. Look in memory for an existing entity that has been loaded from the database or
attached to the context (you’ll learn more about attaching objects in Chapter 4).
2. Look at added objects that have not yet been saved to the database.
3. Look in the database for entities that have not yet been loaded into memory.