Skip to content

Commit

Permalink
Merge pull request OData#529 from OData/robertmclaws-modelproducerfix
Browse files Browse the repository at this point in the history
Fix ModelProducer to handle multiple APIs with different DbContexts
  • Loading branch information
chinadragon0515 authored Dec 27, 2016
2 parents ae7167a + d1e5944 commit fbe309c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,27 @@ public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken can
var dbContext = context.GetApiService<DbContext>();

var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace;
var efEntityContainer = efModel.GetItems<EntityContainer>(DataSpace.CSpace).Single();
// @robertmclaws: The query below actually returns all registered Containers across all registered DbContexts.
// It is likely a bug in some other part of OData. But we can roll with it.
var efEntityContainers = efModel.GetItems<EntityContainer>(DataSpace.CSpace);
// @robertmclaws: Because of the bug above, we should not make any assumptions about what is returned, and get
// the specific container we want to use. Even if the bug gets fixed, the next line should still
// continue to work.
var efEntityContainer = efEntityContainers.FirstOrDefault(c => c.Name == dbContext.GetType().Name);
// @robertmclaws: Now that we're doing a proper FirstOrDefault() instead of a Single(), we wont' crash if more
// than one is returned, and we can check for null and inform the user specifically what happened.
if (efEntityContainer == null)
{
if (efEntityContainers.Count > 1)
{
// @robertmclaws: In this case, we have multiple DbContexts available, but none of them match up.
// Tell the user what we have, and what we were expecting, so they can fix it.
var containerNames = efEntityContainers.Aggregate("", (current, next) => next.Name + ", ");
throw new Exception(string.Format(Resources.MultipleDbContextsExpectedException, containerNames.Substring(0, containerNames.Length - 2), efEntityContainer.Name));
}
// @robertmclaws: In this case, we only had one DbContext available, and if wasn't thw right one.
throw new Exception(string.Format(Resources.DbContextCouldNotBeFoundException, dbContext.GetType().Name, efEntityContainer.Name));
}
var itemCollection = (ObjectItemCollection)efModel.GetItemCollection(DataSpace.OSpace);

foreach (var efEntitySet in efEntityContainer.EntitySets)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -120,12 +120,18 @@
<data name="DataModificationMustBeCUD" xml:space="preserve">
<value>A DataModificationEntry must be either New, Update or Delete.</value>
</data>
<data name="DbContextCouldNotBeFoundException" xml:space="preserve">
<value>Could not find the correct DbContext instance for this EntityFrameworkApi. \r\n The Context name was '{0}' but the Container expects '{1}'.</value>
</data>
<data name="InvalidLineStringGeographyType" xml:space="preserve">
<value>Need 'LineString type', while input is {0}.</value>
</data>
<data name="InvalidPointGeographyType" xml:space="preserve">
<value>Need 'Point type', while input is {0}.</value>
</data>
<data name="MultipleDbContextsExpectedException" xml:space="preserve">
<value>This project has multiple EntityFrameworkApis using different DbContexts, and the correct context could not be loaded. \r\n The contexts available are '{0}' but the Container expects '{1}'.</value>
</data>
<data name="PreconditionCheckFailed" xml:space="preserve">
<value>The precondition check for request {0} on resource {1} is failed.</value>
</data>
Expand All @@ -136,4 +142,4 @@
<data name="UnsupportedPropertyType" xml:space="preserve">
<value>Unsupported type for property: {0}.</value>
</data>
</root>
</root>

0 comments on commit fbe309c

Please sign in to comment.