How do you build OData IEdmModel from Entity Framework model - asp.net-web-api

The title says it all really. I've found several blogs with different ways (serializing the EF model to XML and then de-serializing again to the IEdmModel was one) but they're all based on old version of the OData package.

Serializing is the only way.
Example.
Relevant work.
I've ranted about this a few months ago. AFAIK nothing changed since then, and I personally don't expect them to change too much. The short story is that as of September 2012, there is no plan to use EdmLib in EF, nor is there to use EF's code in other projects.
How much should we align with OData’s EdmLib?
Not worth adopting code
Cost of implementing SSDL & MSL
Freedom to evolve our API independently
Look at aligning names of types and properties where appropriate

If your DbContext is being built from a database-first approach the given answer will fail giving this error:
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
After some time messing with this I have found an appropriate solution. Basically you grab the CSDL resource from the assembly containing the DbContext in question and parse it using the Microsoft.Data.Edm.Csdl.CsdlReader.TryParse method. The resulting IEdmModel is valid containing the exact information given by EntityFramework when the model was built.
See here for an example with usage

Related

jaydata/jaysvcutil 1.3.5 inverseProperty support for WebAPI

I've seen Missing inverse property in asp.net webapi odata $metadata and the WebAPI $metadata I'm dealing with behaves as described in this article: it doesn't reuse associations for bi-directional navigation properties.
When using jaysvcutil 1.3.5 all navigational properties come up as $$unbound.
$data.Entity.extend('API.Models.Document', {
...
'Document_Versions': {
'type':'Array',
'elementType':'API.Models.Document_Versions',
'inverseProperty':'$$unbound' }
});
Other than manually updating the inverseProperty information is there anything to get the desired result automatically?
Update based on #Robesz answer
Manually adding inverseProperty information to the static .js converted by JaySvcUtil is doable, but I'm asking if there's an option to accomplish that with the dynamic conversion as well.
There seems to be to options
make modifications to the .NET WebAPI. Might be challenging, because their seem to be good reason for their implementation, but maybe somebody already successfully did that.
modifying the conversion XSLT that JayData using to take that behavior into account.
We've just arrived to the same results with WebAPI OData, but after editing the generated context file manually and adding the inverseProperty everything is working fine
This should be most probably handled by extending JayData's XSLT conversion. I've created an issues for that on https://github.com/jaydata/jaydata/issues/155.

Stored procedure in Entity framework with multiple parameters in mvc3

I am new in entity framework and mvc3.how can i execute Stored procedure in Entity framework with multiple parameters in mvc3.
This depends if you are using code first or not. I do agree with the folks above you could've probably easily researched this but I'll provide an overview here so it's all in one spot to review as there are several options to consider.
With code first in the latest version of EF 6 Beta, you can execute stored procs as part of code-first as described in the docs here
Prior to EFv6 you could use the following to execute a proc in your code and map to an entitycontext.Database.SqlQuery<YourEntityType>
If you aren't using code first, you can map in the designer as explained by Julie Lerman many places, but one easy link is here

Returning child elements in ASP.NET WebAPI OData

I'm using the latest ASP.Net WebAPI Nightly builds (dated 2013-01-16).
I have a simple EF database first model at the moment that has two entities - Patients and Visits. Each patient can have many visits.
I'd like to be able to query for my list of patients and have the visits entities for each patient returned inline. I know that WebAPI's OData implementation doesn't yet support $expand. I'm hoping that just means that optional client-controlled expansion is not supported and that I can force expansion server-side.
At the moment I'm not getting any of the visits inline.
For example, my PatientController's() Get() method looks like
[Queryable(AllowedQueryOptions=AllowedQueryOptions.Supported)]
public override IQueryable<Patient> Get()
{
var query = this.entities.Patients.Include("Visits");
return query;
}
I've verified that the query executing against my database does indeed include the visit information.
To use a publicly available OData service as an example, if you use the service at http://services.odata.org/OData/OData.svc/, you can get a list of Suppliers. This is http://http://services.odata.org/OData/OData.svc/Suppliers.
You can also ask for a list of suppliers that includes the list of products using http://http://services.odata.org/OData/OData.svc/Suppliers?$expand=Products
Stepping through the ASP.NET code (via the symbols server) I've got to the System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer and can see that it's CreatePropertyBag method, which builds up the list of properties to be serialized, just doesn't include the navigation properties, and they don't seem to be enumerated anywhere else apart from being written out as NavigationLinks.
I'm quite new to the ASP.NET world in general and have spent a week or so getting my head around the way things work (particularly with the changes made to OData at the end of 2012 and further changes made so far in 2013).
I suspect that if the ODataEntityTypeSerializer was to be modified (I'm happy to try) to embed this extra information in the appropriate spot (within each navigation link as an nested inline feed as best I can tell) then I'd be set.
Questions:
Have I overlooked something obvious and there's a flag I can set to turn on this behaviour? I can see why, if such a flag exists, it would be off by default (EF lazy loading and this flag wouldn't get on well)
If #1 is no, is there some other ODataEntityTypeSerializer that I could use? If so, how do I switch to it?
If #2 is no, any pointers for where I should start writing my own? Is there a place I can substitute in my own serializer or do I have to maintain my own fork of ASP.NET's Extensions project (as opposed to the Runtime project)
Thanks very much!
$expand is very high on our list of things to support for OData. But as far as I know, we don't have any flag to turn it on server-side. The formatter doesn't currently allow you to substitute your own serializers either. So I'm afraid your only option in the meantime is to create a fork and add support for $expand. If you manage to get it working, please consider sending a pull request our way:
http://aspnetwebstack.codeplex.com/SourceControl/network
You can try it already in webapi nightly builds.
Here is how to install it with nuget:
http://aspnetwebstack.codeplex.com/wikipage?title=Use%20Nightly%20Builds

How to get MvcScaffold to work with external models?

Steve Sanderson has a great blog that goes into the details of MvcScaffolding. See it here http://blog.stevensanderson.com/2011/01/13/mvcscaffolding-standard-usage/
He also has a great video on http://channel9.msdn.com/Series/mvcConf/mvcConf-2-Steve-Sanderson-MvcScaffolding
In his blog he mentions….
ModelType : By default we try to infer this from the controller name, but if you want your controllers and models to have totally unrelated names, you can specify a particular model type using this parameter. We’ll find any model class defined in your project or another project it references, but not in any external assembly.
Our ViewModels are kept in a separate project in the same solution. However we are Referencing the DLL so Get-ProjectType –ModelType ourVM –Project OurProject does not find the model.
No problem it is an open source project. I will down the code and tweek Get-ProjectType.
I am unable to find where Get-ProjectType is defined in PowerShell. (resolved)
We used MvcScaffold to create custom Scaffolders ( see steve sanderson video during MvcConf 2). Then our custom scaffolder called our custom powershell scripts to do the work.

How to generate BlToolkit model

From what I have seen on the ormbattle.net, BlToolkit is the fastest ORM for .NET. I would like to test it in one of my projects, but I don't know how to generate entity classes. Should I do this by hand? I have 20+ tables in the database.
Is there any online tutorial or blog, where I can find more information about BlToolkit?
It looks like a library with great potential without community behind it.
you can use our project on bltoolkit code generation:
http://sogen.codeplex.com/
There are the following options:
T4 templates are available for some databases - http://www.bltoolkit.net/Doc.T4Templates.ashx
MyGeneration and template for BLToolkit.
Classes generated for Linq 2 SQL. BLToolkit can use L2S metadata.

Resources