abe545 / CodeOnlyStoredProcedures

A library for easily calling Stored Procedures in .NET using only code (no xml or gui).
MIT License
4 stars 3 forks source link

Support for EF Fluent API #90

Open leevva opened 7 years ago

leevva commented 7 years ago

Seems it can't be used fluent API configuration for mapping fields. Such as http://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx

abe545 commented 7 years ago

No, I didn't create a fluent api for mapping columns to C# properties. I agree that it is something that would be nice to have. Based on your title, it sounds like you'd want to be able to pass a DbModelBuilder to the fluent api to map your POCO. Or would you be okay with a similar api in this library? I'm not sure how open the EF classes are to getting the entity descriptions out, so it would take a bit of work to determine (and I would definitely add it to a separate library, as I don't really want to tie this to EF in general - I know it is for .NET 4.0 code, but I didn't want to re-invent those mapping attributes).

leevva commented 7 years ago

For mapping properties of my classes i don't use ColumnAttribute but i use HasColumnName method of EntityFramework fluent api. When stored procedure returns result CodeOnlyStoredProcedures throws exception:

Result Message:
CodeOnlyStoredProcedure.StoredProcedureResultsException : No columns with name all_properties_of_my_class were found in the result set for type my_type. This property will be ignored if it is decorated with a NotMappedAttribute. You can also map the property to a different column in the result set with the ColumnAttribute. If the stored procedure can sometimes return the column, decorate the column with the OptionalAttribute.

Result StackTrace:
at CodeOnlyStoredProcedure.RowFactory.ComplexTypeRowFactory1.CreateRowFactory(IDataReader reader, IEnumerable1 xFormers) at CodeOnlyStoredProcedure.RowFactory1.ParseRows(IDataReader reader, IEnumerable1 dataTransformers, CancellationToken token) at CodeOnlyStoredProcedure.StoredProcedure3.Execute(IDbConnection connection, CancellationToken token, Int32 timeout) at CodeOnlyStoredProcedure.StoredProcedure3.Execute(IDbConnection connection, Int32 timeout)

EntityFramework stores mapping in DbContext. It can be retreived this way https://romiller.com/2015/08/05/ef6-1-get-mapping-between-properties-and-columns/

I can't use ColumnAttribute because classes are in domain and they're isolated from data layer. It's not convinient to create extra classes for result sets.

leevva commented 7 years ago

I've found the reason: http://stackoverflow.com/questions/14733047/entity-framework-code-first-configure-mapping-for-sqlquery

abe545 commented 7 years ago

Yes, I hear what you're saying. Like I said, it would be a good feature to add as a separate library that is dependent on both EF and CodeOnlyStoredProcedures. In fact, even if the EF folks did some work to support column mappings in a SQL query, CodeOnlyStoredProcedures wouldn't pick it up. My library constructs all the queries itself, so there would need to be an extra mapping layer that looks at the EF fluent mappings.

It is doable, but it will take some time. It is going to require some work that I've been wanting to do to improve the architecture of the mapping between the results and the returned objects. This work will likely take some time, so I can't really give you an estimate on when I'll have something ready, unfortunately.

leevva commented 7 years ago

Ok, i see. Thanks