Programmatically import stored procedures in T4 in EF 5 - entity-framework

I have a T4 template that generates the edmx model of my DB; I have to use this T4 template because i have to define my own localized pluralization dictionary to manage plurals/singulars.
The bad thing is that i have to manually import my stored procedures every time, after i launched my T4 template, via the 'import function' menu.
Is there a way to import the stored procedures programmatically?
Is the code of the "import function" wizard available and open source?
Mostly i'd like to see how they implemented the 'obtain column informations' to generate the result class.

The EDMX file is just XML. You can programmatically open and edit that XML file as you would any other.
I would imagine that "Obtain column information" works by using ADO to query the parameters, (see http://support.microsoft.com/kb/174223 ), populating them with dummy values, and querying the result - I think that would work, but perhaps SQL profiler would reveal more.

Related

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.

How-to edit EDMX programmatically

I'm using a database-first process. I've got hundreds of tables imported into my EDMX and generated CUD procs imported as well. The generated CUD procs use a naming convention based on the table name and he CUD operation.
How can I write some code to open the EDMX, associate the CUD procs with the proper entities, and save the EDMX?
This is a lazy-developer, design-time task - this will not be happening at runtime.
Well, what I wanted was a free library which contained the object-model representation of the EDMX. This would allow developers to edmx.Load(pathToEdmx), query the object-model, make changes, and edmx.Save(). Apparently, such a library doesn't exist.
So, I wrote some code to load the XML, query the XML, make some changes, and save. Not as rich as an object-model with validation and helpful exceptions (and very prone to typos), but it worked.

Entity Framework Code Generation

I'm trying to figure out what the best way to go would be for Entity Framework code generation with a "database first" approach. I want the complete master description of the model to be the actual database schema with annotations stored in database in tables or extended properties. Not interested in a model first or code first approach. Not opposed to writing custom code to implement if it will yield the highest quality solution. Here are some ideas:
(1) Generate EDMX from the database. Use custom code to add annotations to the EDMX regarding uniqueness and other constraints that are not currently built in to EF. Use T4 templates to generate code from the EDMX.
(2) Write a custom tool that generates "code first" classes from the database. No EDMX files in the solution.
(3) Something else?
It seems like #1 requires deeper knowledge of EF and more actual work than #2, which I am now opposed to if there are significant benefits.
Any advice?
Well, nothing is easier than just dragging the tables from the server explorer to the edmx designer surface. That is how I do it. I don't do any customizations to the model so I can just delete parts of it an regenerate.
You can import the model from your database with EF. Right-click on the EF designer and choose "update model from database" option.
If you want a little more you can try http://radarc.net . It's a code generation tool which creates scaffolding and CRUDs. It uses T4 templates and code first so you can customize the way it generates code.

Imported a sproc in EF4 but no code was generated

I have a stored proc in my database and I am using Entity Framework 4 to work with the database from my code. Following instructions for importing a sproc, I was able to add it to my model and import a function to reference it. In the model browser under Function Imports, the stored proc and all its parameters appears. The access says "Public." Yet, I am not able to see it in Intellisense when I go to call it in my context object. When I do a global search on the name of the sproc, I see it appears in 3 places in the EDMX, and once in my calling code, but no code appears to have been generated for it in the Designer.cs file.
I must have missed a step or something, but as I look back through it, it's all pretty simple; I can't see where I went wrong. Has this happened to anyone? How can I troubleshoot?
if you switch to the model browser view on the .edmx file, you can see your imported SProcs under the model store node in the stored procedures folder. You can right-click them there and select "add function import" to generate the execution code in the entity data model's Designer.cs file. Unfortunately, you seem to have to do this import for them individually. If someone knows how to import them all simultaneously, I'd love to know.
Is that what you're looking for?
Take a look at this MSDN thread. On this forum, there is another thread addressing this issue in some way.
In short, it is an EF Designer limitation. You can try writing your own code to call this function, or use third-party designer like Entity Developer.

Entity framework 3.5, mapping stored procedure results to custom entity

I tried to follow this link to map stored procedures to Custom Entities, but when I did so I could no open the EDMX file in the VS 2008 designer.
so looks like it is causing some issues.
Does anyone know how to map Stored procedure results to custom Entities in Entity Framework?
In VS2010 and EF4 this is extremely easy todo.
When you do a function import it there is an extra option for returning the collection as a complex type. Even better there is a feature # the bottom of the 'Add Function Import' dialog that will attempt get the columns from the stored proc result set. Finally, it gives you the option todo a one-click 'Create New Complex' type based upon the result sets. It appears the EF team saw this as a pain and took all necessary steps to make this easier.
We have a large project that we are about to start on (large meaning 30+ developers and taking 2+ years to complete). We were weighing the options of using standard ADO.Net, EntityFramwork, or a 3rd party ORM like LLBLGen. What we are seeing so far is that the Entity Framework 4 release is a much more full featured ORM. I would be very skeptical # using EF in VS2008 mostly because of the issue you are talking about in this post + how EF handled the FK's in the VS2008 release (http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx)
I just successfully solved this problem I was having after following the same walkthrough link you provided. (I suppose this answer is for anyone who finds this unanswered question in their search.)
My problem was that I was not opening the ssdl file correctly. You MUST open it by right clicking the edmx file in the solutions window --> "open with" --> "xml (text) editor" or else the file will not be built properly after making changes, and you won't be able to see the graphical representation of your model. As long as you complete his tutorial editing ONLY the ssdl section of the file, it will work. Just be careful. Opening the ssdl portion of this file in external programs such as Notepad ++ WILL NOT WORK.
Hope that helps someone. My problem is now passing SelectedItem property of a listbox displaying an entity query result as a parameter to my makeshift st