EF 6 Database First - map stored procedure to existing objects - entity-framework

Hi I have a project which is quite old. It uses stored procedures which isn't an issue but it does have lots of error prone mapping code in old ADO.net. My main goal is to cut down the mapping more than anything. We have a separate library of I guess POCO/DTO type objects that get mapped to.
Can I map stored procedures to these classes easily. I suppose I could use AutoMapper as the mapping will be exact but I'm wondering if there's an easier way that it does out of the box. The list of complex type doesn't show the imported library's business classes. Cheers.

Instead of using AutoMapper, create a complex return type using "Edit Function Import", which has the exact properties as the stored procedure result.

Related

Entity framework code first and stored proc

I apologize in advance as a I feel the answer to this question is out there, but I can't word my query properly, so I don't get much back.
Anyhow - I have a project where I mostly have a new data architecture aside from literally a couple of tables. I'd like to use EF code first. So my question is - is it possible to use code first on the majority of the project aside from a couple of classes for I which I can define stored procedures to go after tables that already exist?
Thank you
You can access the ObjectContext property of your DbContext instance in order to execute a stored procedure like so:
((IObjectContextAdapter)myDbContext).Objectcontext.ExecuteStoreQuery<ResponseType>(
"EXEC MyStoredProc #Value1, #Value2",
new SqlParameter("#Value1", value1),
new SqlParameter("#Value2", value2)
)
However, even if the tables exist, all you need is classes that match the tables - CodeFirst does not literally mean that the code has to exist first. As long as your classes match the table structure and name (or you use the fluent api or annotations to make them match), you'll be fine. Unless the stored procedures are doing something special that is hard to do in EF, I would stick to using EF consistently.

Get values in NotMapped property in model class Entity Framwork Code First using linq

I have the below scenario. I am using EF 5 Code first, MVC 4 on VS 2010. I am using the Unit of Work and Repository pattern for my project.
I am not sure if this is possible or not. Kindly suggest.
I have a model class representing a database table. In the model class, I have a property that is decorated as [NotMapped]. I have a Stored Proc that returns data, similar to the model class. However, when I get the data in a List from the SP, it does not contain value for the [NotMapped] column (SP returns data for the [NotMapped] column though). This may be logically correct with respect to EF.
All I want to know is, do we have a way to get data populated for the [NotMapped] column. I want to achieve, CRUD using LINQ (excluding R - Read).
I would recommend to create a separate complex type for the stored procedure results. Otherwise sooner or later you will find yourself writing code to distinguish between entities coming from the DbSet or from the stored procedure. When the come from the stored procedure they can't be used in joins, for example. Or checks whether or not the unmapped property is set.
A very dirty approach could be to have two different contexts. With code first it is possible to have different contexts with different mappings to the same types, with and without the column ignored (if you use fluent mapping, not with data annotations). But that only succeeds if you tell EF not to check the database schema, so using migrations is ruled out as well. I would not do it!! For the same reason as I mentioned above. I would hate to have a type with a property that sometimes is and sometimes isn't set.

Returning anonymous types from stored procedure

I have an existing database with a set of stored procedures. I am redesigning the application layer without making changes to database objects. One of the difficulties I am faced with is there are many stored procedures which are similar to each other in that they query the same tables but return different combinations of columns.
I am neither able to return partially filled entities nor I could find a way to return anonymous types from stored procedures using Entity Framework 4.1 (and SQL Server 2008 R2). This is forcing me to define too many complex types one per stored procedure although underlying table structure is the same.
My questions are :
Please suggest solutions for minimising number of entities / complex types which can be implemented using EF and without making changes to database objects.
Also, is it possible to return partially filled entities? This would enable me to reuse entities. I am not planning to use object tracking features.
How can we return anonymous types from stored procedures outputs? This is also good enough for me since in most cases I am going to return the data to client in JSON format
Thanks
You won't be able to return anonymous types - those are typically restricted to the scope of the method they're defined inside of.
Returning partially filled types isn't possible per se - but you could do this:
in your database layer, call the stored procedure in question; it will return a specific complex type to match its "signature"
using something like AutoMapper, you could easily copy those returned fields in the complex type into an entity of your system, thus getting a "partially filled" entity
return that entity from your database layer to the application to be used
You won't be able to avoid having lots of complex types just for the sake of getting the return values from the stored procedures - but you'll keep those locked away inside your database layer, they won't "leak out" into your entire application.
Just use System.Data.DataTable like good old days.

Replacements to hand-rolled ADO.NET POCO mapping?

I have written a wrapper around ADO.NET's DbProviderFactory that I use extensively throughout my applications. I also have written a lot of code that maps IDataReader rows to POCOs. However, as I have tons of classes the whole thing is getting to be a pain in the ass to maintain.
I have been looking at replacing the whole she-bang with a micro-orm like Petapoco. I have a few queries though:
I have lots of POCOs that contain other POCOs in them as properties. How well does the Petapoco support this?
Should I use a ORM like Massive or Simple.Data that returns a dynamic object and map that to a POCO?
Are there any approaches I can take to the whole mapping of rows to POCOs? I can't really use convention-based tools as my database isn't particularly consistent in how it is designed.
How about using a text templating/code generator to build out a lightweight persistence layer? I have a battle-hardened open source project called TextMetal to generate the necessary persistence layer based on tried and true architectural decisions. The only lacking thing is object to object relations but it does support query expressions and works well with poorly designed data schemas.
You can see a real world project that uses the above tool call Can Do It For.
Feel free to ask me about any design decisions once you take a look-sse.
Simple.Data automagically casts its dynamic type to static types. It will map nested properties as long as they have been eager-loaded using the .With method. So for example
Customer customer = db.Customer.WithOrders().Get(42);
would populate the Orders property of the customer object.
Could you use QueryFirst, or modify it? It takes your sql and wraps it in vanilla ADO code, generated at design time. You get fresh POCOs from your result schema every time you save your file. Additionally, you can choose to test all queries and regenerate all wrappers via the option in the tools menu. It's dependent on Sql Server and SqlClient, so unless you do some modification, you'll lose DbProviderFactory.

Can I map incomplete sproc columns to an entity?

I am using the latest beta of Visual Studio 2010 and the Entity Framework. It's mostly really neat, but here's my situation:
I have a table T with columns Id and Name.
I have an auto-generated entity with Id and Name properties.
Finally, I have a stored procedure that selects only Id from T.
Trying to map my entity to the stored procedure results in an EntityCommandExecutionException:
A member of [the entity], 'Name', does not have a corresponding column in the data reader with the same name.
That makes sense, but is there some way to partially populate my entity from the stored procedure call and then fully materialize it later with a second query?
Nine months ago, the answer to this question appeared to involve a great deal of manual labor. In my case, we have hundreds of stored procedures, and literally none of them return full rows. The Entity Framework has come a long way since then, so I am hoping something might've changed.
Thank you in advance for any help!
One approach might be to map the procedure results into a complex type, and then customize the code generation to add a method to this type which will materialize the entire object.
One possible hitch with this idea is that I'm not sure it's possible to customize code generation for complex types. You can certainly customize code generation for entity types, as explained in great detail in this post. It seems like you should be able to customize complex types, as well, but I've never tried it.