entity framework 3.5 and stored procedure result mapping - entity-framework

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.

Related

EF6 Updating database from model overwrites my DefiningQuery

I'm using DefiningQuery to specify a Read only "view" entity that contains fields from two tables, and it works great for my need.
But each time I'm updating my model (using the Model designer) and runs the "Generate Database from Model..." operation, my DefiningQuery is lost because the .edmx file is getting overwritten.
The SQL being generated (in the .edmx.sql file) now assumes my Read Only entity must have a corresponding table created.
Is there a way to avoid the DefiningQuery being overwritten when the model is updated?
If possible I would like to avoid having to generate a Database View and map to that.
A little info:
I'm working on a project based on the Entity Framework 6.x.
I'm using the Model First approach.
The database is an SQL Server.

Generating SQL Server Views from EDMX using T4 templates

I'm working with a legacy database that I can't easily create an entity model over because it uses extension tables with what is effectively composite keys and EF only supports single column keys for mapping one entity to multiple tables.
So, what I've decided to do is create updatable views (with INSTEAD OF triggers to handle CRUD operations) over the top of the legacy tables (which cannot be touched) and then have my entity model (either using EF or DevExpress XPO) built on top of the database views. This will also allow me to do stuff like easily add sub-queries in the select clause to retrieve child counts on parent records when retrieving a list of parent records in a single query.
However, I don't particularly want to manually write the SQL for all the views and triggers so I thought I'd use data model defined in the .EDMX file and t4 templates to help me generate the bulk of the T-SQL needed to create the views and the triggers. I thought there would be some template that I could use as the basis for doing this, but seems that's not so easy to find.
Can someone please suggest a t4 template that I could use as the basis where mappings are being retrieved from the .EDMX. Alternatively can anyone advise how to use the StorageMappingItemCollection to retrieve the mapping information from the EDMX file. I know a few people have said that apparently you can't use it or that they just use Linq to Xml, but I would have thought it should certainly be possible to use the StorageMappingItemCollection class as a strongly typed class to access this data.
Any examples of how I could use StorageMappingItemCollection to access mapping info would be very helpful. Thanks.
See http://brewdawg.github.io/Tiraggo.Edmx/ you can install it via NuGet within Visual Studio and it serves up all of the metadata from your EDMX files that Microsoft hides from you, very simple, works great.

Integrating a Stored Procedure in UnitOfWork/Repository pattern

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.

Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase

I'm using the SQLite provider for Entity Framework 5 but it doesn't support CreateDatabase and thus cannot auto create the database. (Code First)
Is there a way I can obtain the Model schema at runtime so that I can create the SQL "CREATE TABLE" command myself?
If not at runtime, some other way to obtain the schema so I know how to create the table properly?
Thanks!
A) As for obtaining the model schema at runtime part
(all are earlier posts of mine)
See this one How I can read EF DbContext metadata programmatically?
And How check by unit test that properties mark as computed in ORM model?
Also this one for a custom initializer Programmatic data transformation in EF5 Code First migration
Having said that...
The problem I see is where and at what point you actually have the data available.
Actually I'm quite sure you won't be able to do that at any time.
Because to be able to extract that info you need to have a DbContext running - so db has to be constructed etc. etc.
In the initializer maybe - but using different ways to get that info - the above is not available.
B)
The other way would be to go the way of implementing a provider, generator etc. (e.g. this post).
That way you should get all that info just at the right time from the EF/CF itself.
However I haven't played with that much.
For more info you can check the EF source code
This is more of a 'gathered info' so far - in case it helps you get anywhere with it. Not really a solution. I'll add some more tomorrow.
EDIT:
To get the real database metadata, look into the other DataSpace, this should get you to the right place...
(note: things tend to get less exact from here - as obviously there isn't the right official support)
var ssSpaceSet = objectContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace).First()
.BaseEntitySets
.First(meta => meta.ElementType.Name == "YourTableName");
If you look up in debugger, Table property should have the real table name.
However, reflection might be required.
How I can read EF DbContext metadata programmatically?
How check by unit test that properties mark as computed in ORM model?
Programmatic data transformation in EF5 Code First migration
Entity Framework MigrationSqlGenerator for SQLite
http://entityframework.codeplex.com/
Entity Framework - Get Table name from the Entity
ef code first: get entity table name without dataannotations
Get Database Table Name from Entity Framework MetaData
http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

Defining business objects in Entity Framework

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.