I've got an application with MVC and Entity Framework. The application uses Unit of Work and Repository patterns for CRUD operations. But I've got to add now a couple of stored procedures that already exist in database. One of them just retrieves data from one of the entities (this is achieved at this moment by the repository pattern) but adds an extra column to the final result, created and populated in the stored procedure.
I want to integrate the use of these stored procedures into my architecture. I've tried to add the stored procedures to my model, map it to the class and use it, but as I have to add an extra column to this entity in the model, I get an error that this field is not mapped.
Should I use my repository for this particular entity just for Add/Edit/Delete and create another entity with the extra field that will be used for just the Get action using the stored procedure?
Thanks.
Should I use my repository for this particular entity just for Add/Edit/Delete and create another entity with the extra field that will be used for just the Get action using the stored procedure
Depends on the use case? Sounds like it's used for a different case and if so I would create a new entity for it.
Related
I am developing a SPA using Angular-Breeze-WebAPI-EntityFramework.
Now Breeze uses the Entity Framework metadata information to create it's own Breeze models. We use this in our application for Breeze validation.
So far, it's all been nice and easy. Now we are having to create a search page (say for querying customers). The search can be by Customer.Name or by Product.Id (which would return a list of customers who have bought that product). The result is a ng-repeater, which displays Customer.Name, Order.LastPlaced etc.
if you are getting confused by the tables and columns, forget that. What I am only trying to get to is that, both the search object and the result object are not 1:1 with Entity tables (or objects). So obviously I feel the need to create a custom object (one for the search and one for the results). My question primarily is where and how do I create that object?
If I create it at the data layer, Breeze would have no idea of the metadata for each of the properties (since it uses EF for that).
I obviously can't create just a JavaScript object, since I will have to query the database (using EF) to search and populate the object.
So where does one create such a custom object (traversing multiple tables) such that Breeze still can figure out the metadata and perform validation and such when the need arises?
Thank you all.
You can create metadata on the client for types that the server either doesn't know about or doesn't have the schema for. See http://www.breezejs.com/documentation/metadata-by-hand.
I am implementing data mapper in my zend framework 1.12 project and its working fine as expected. Now further more to enhance it i wants to optimize it in following way.
While fetching any data what id i wants to fetch any 3 field data out of 10 fields in my model table? - The current issue is if i fetches the only required values then other valus in domain object class remains blank and while saving that data i am saving while model object not a single field value.
Can any one suggest the efficient way of doing this so that i can fetch/update only required values and no need to fetch all field data to update the record.
If property is NULL ignore it when crafting the update? If NULLs are valid values, then I think you would need to track loaded/dirty states per property.
How do you go about white-listing the fields to retrieve when making the call to the mapper? If you can persist that information I think it would make sense to leverage that knowledge when going to craft the update.
I don't typically go down this path. I will lazy load certain fields on a model when it makes sense, but I don't allow loading parts of the object like this, rather I create an alternate object for use in rendering a list when loading the full object is too resource intensive. A generic dummy list object I just use with tabular data. It being populated from SQL or stored procedures result-sets, usually with my generic table mapper.
I am looking for a way to map stored procedures logically related to a table as functions within the Entity generated for that specific table instead of function in overall Context.
As an example, I have generated edmx out of existing bank database and I got a Context and several entities corresponding to the tables. Assume we have tables for Account, Transactions, Address, etc... and I have a stored proc returning current balance for the account. I want to map this stored proc to the Account entity instead of the context. This will help me to call Account.GetBalance() instead of Context.GetBalance().
Is this possible in entity framework ? I did lot of search and read few articles/blogs in msdn but couldn't find any solution for this.
No it is not possible unless you manually wrap the method exposed on context in your entity - that would make your entities persistence aware and EF dependent which is currently considered as exact opposite of design people should follow.
In terms of EF balance should be property of account entity and retrieving balance should mean reloading account or executing projection query (or your procedure) on accounts set / context. Queries are executed from context and stored procedures as well.
Trying to understand Entity Framework. My approach is database first. However I would like to define other entites in the model that is closer to my business objects. I guess I could write queries in the db and include them in the model. But I would also like to define entirely new entities in the model though they would be based on underlying tables in the db. How do I do that - does anyone know a tutorial?
Regards
Bjørn
db Oldtimer, EF Newbie
Database first means that you have existing database and you can either create model by updating from database or manually. You can use wizard to create initial model and modify it manually to define new entities but you must not use update from database any more or some of your changes will be deleted. Also your custom modifications must follow EF mapping rules (for example it is not directly possible to map multiple entities to the same table except some more advanced mapping scenarios like splitting and inheritance) and some of them (custom queries) must be done directly in EDMX source (XML) because designer doesn't support them - this requires more complex knowledge of EF mapping and it will be definitely hard for newbie.
You can check specification of that XML. For entities mapped to custom queries you will have to use DefiningQuery element in SSDL part of EDMX.
Using EF3.5 with Visual Studio 2010 (cannot upgrade to EF4 at this point - don't ask!).
Wanting to create a stored procedure that aggregates some fields from some related tables and materialize the result of the stored procedure as a custom "entity". This custom entity would be "read only". I set up the custom entity, the stored procedure, and function import. When I build my Entity project, I get the following:
Error 35 Error 3027:
No mapping specified for the following
EntitySet/AssociationSet -
MyCustomEntitySet
It looks like it wants a table mapping defined for my custom entity, however, I would not have one in this case since it aggregates the data over several tables (and filters out some unneccessary data).
Is it possible to map a custom entity to a stored procedure? Is it possible to do so in a way where the "Update Model From Database" functionality will not break the custom entity or stored proc/function import mapping?
TIA!
We used to get around this by creating a view in the db - the view never actually gets used if you map to stored procedures but it does enable auto-creation of the correct mappings in the entity.
Typically for fairly straight forward procs you can copy/paste the sql to generate the view too - saves some time.