-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EdmBuilder.cs and its Breeze.EdmBuilder nuget package
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Data.Entity; | ||
using System.IO; | ||
using System.Xml; | ||
using Microsoft.Data.Edm.Csdl; | ||
|
||
namespace Microsoft.Data.Edm | ||
{ | ||
/// <summary> | ||
/// DbContext extension that builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/> | ||
/// </summary> | ||
/// <remarks> | ||
/// We need the EDM both to define the Web API OData route and as a | ||
/// source of metadata for the Breeze client. | ||
/// The Web API OData literature recommends the | ||
/// <see cref="System.Web.Http.OData.Builder.ODataConventionModelBuilder"/>. | ||
/// That component is suffient for route definition but fails as a source of | ||
/// metadata for Breeze because (as of this writing) it neglects to include the | ||
/// foreign key definitions Breeze requires to maintain navigation properties | ||
/// of client-side JavaScript entities. | ||
/// <p>This EDM Builder ask the EF DbContext to supply the metadata which | ||
/// satisfy both route definition and Breeze.</p> | ||
/// </remarks> | ||
public static class EdmBuilder | ||
{ | ||
/// <summary> | ||
/// Builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/> | ||
/// </summary> | ||
/// <example> | ||
/// /* In the WebApiConfig.cs */ | ||
/// using (var context = new TodoListContext()) | ||
/// { | ||
/// config.Routes.MapODataRoute( | ||
/// routeName: "odata", | ||
/// routePrefix: "odata", | ||
/// model: context.GetEdm(), | ||
/// batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer) | ||
/// ); | ||
/// } | ||
/// </example> | ||
/// <param name="dbContext">The source <see cref="DbContext"/></param> | ||
/// <returns>An XML <see cref="IEdmModel"/> </returns> | ||
public static IEdmModel GetEdm(this DbContext dbContext) | ||
{ | ||
using (var stream = new MemoryStream()) | ||
{ | ||
using (var writer = XmlWriter.Create(stream)) | ||
{ | ||
System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(dbContext, writer); | ||
} | ||
stream.Position = 0; | ||
using (var reader = XmlReader.Create(stream)) | ||
{ | ||
return EdmxReader.Parse(reader); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"> | ||
<metadata> | ||
<id>Breeze.EdmBuilder</id> | ||
<version>1.0.0</version> | ||
<title>Breeze Labs: EdmBuilder</title> | ||
<authors>Ward Bell</authors> | ||
<owners>Ward Bell</owners> | ||
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl> | ||
<projectUrl>http://www.breezejs.com/breeze-labs/breezeedmbuilder</projectUrl> | ||
<iconUrl>http://www.breezejs.com/sites/all/images/breezeb.png</iconUrl> | ||
<requireLicenseAcceptance>false</requireLicenseAcceptance> | ||
<description>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext. This EDM can produce the metadata Breeze needs to manage a corresponding JavaScript entity model on the client including the foreign key properties necessary to maintaining associations. This builder replaces the ODataConventionModelBuilder in a Web API OData application. See the comments in the code for an explanation and usage. See it in action in | ||
the "Breeze Web API OData Sample".</description> | ||
<summary>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext.</summary> | ||
<copyright>Copyright © 2014 IdeaBlade</copyright> | ||
<language>en-US</language> | ||
<tags>breeze webapi odata</tags> | ||
</metadata> | ||
</package> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"> | ||
<metadata> | ||
<id>Breeze.EdmBuilder</id> | ||
<version>1.0.0</version> | ||
<title>Breeze Labs: EdmBuilder</title> | ||
<authors>Ward Bell</authors> | ||
<owners>Ward Bell</owners> | ||
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl> | ||
<projectUrl>http://www.breezejs.com/breeze-labs/breezeedmbuilder</projectUrl> | ||
<iconUrl>http://www.breezejs.com/sites/all/images/breezeb.png</iconUrl> | ||
<requireLicenseAcceptance>false</requireLicenseAcceptance> | ||
<description>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext. This EDM can produce the metadata Breeze needs to manage a corresponding JavaScript entity model on the client including the foreign key properties necessary to maintaining associations. This builder replaces the ODataConventionModelBuilder in a Web API OData application. See the comments in the code for an explanation and usage. See it in action in | ||
the "Breeze Web API OData Sample".</description> | ||
<summary>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext.</summary> | ||
<copyright>Copyright © 2014 IdeaBlade</copyright> | ||
<language>en-US</language> | ||
<tags>breeze webapi odata</tags> | ||
</metadata> | ||
</package> |
58 changes: 58 additions & 0 deletions
58
Nuget.builds/Breeze.EdmBuilder/content/App_Data/EdmBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Data.Entity; | ||
using System.IO; | ||
using System.Xml; | ||
using Microsoft.Data.Edm.Csdl; | ||
|
||
namespace Microsoft.Data.Edm | ||
{ | ||
/// <summary> | ||
/// DbContext extension that builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/> | ||
/// </summary> | ||
/// <remarks> | ||
/// We need the EDM both to define the Web API OData route and as a | ||
/// source of metadata for the Breeze client. | ||
/// The Web API OData literature recommends the | ||
/// <see cref="System.Web.Http.OData.Builder.ODataConventionModelBuilder"/>. | ||
/// That component is suffient for route definition but fails as a source of | ||
/// metadata for Breeze because (as of this writing) it neglects to include the | ||
/// foreign key definitions Breeze requires to maintain navigation properties | ||
/// of client-side JavaScript entities. | ||
/// <p>This EDM Builder ask the EF DbContext to supply the metadata which | ||
/// satisfy both route definition and Breeze.</p> | ||
/// </remarks> | ||
public static class EdmBuilder | ||
{ | ||
/// <summary> | ||
/// Builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/> | ||
/// </summary> | ||
/// <example> | ||
/// /* In the WebApiConfig.cs */ | ||
/// using (var context = new TodoListContext()) | ||
/// { | ||
/// config.Routes.MapODataRoute( | ||
/// routeName: "odata", | ||
/// routePrefix: "odata", | ||
/// model: context.GetEdm(), | ||
/// batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer) | ||
/// ); | ||
/// } | ||
/// </example> | ||
/// <param name="dbContext">The source <see cref="DbContext"/></param> | ||
/// <returns>An XML <see cref="IEdmModel"/> </returns> | ||
public static IEdmModel GetEdm(this DbContext dbContext) | ||
{ | ||
using (var stream = new MemoryStream()) | ||
{ | ||
using (var writer = XmlWriter.Create(stream)) | ||
{ | ||
System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(dbContext, writer); | ||
} | ||
stream.Position = 0; | ||
using (var reader = XmlReader.Create(stream)) | ||
{ | ||
return EdmxReader.Parse(reader); | ||
} | ||
} | ||
} | ||
} | ||
} |