diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs index b67228b9..3e59ff4d 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Model/ModelProducer.cs @@ -62,7 +62,27 @@ public Task GetModelAsync(ModelContext context, CancellationToken can var dbContext = context.GetApiService(); var efModel = (dbContext as IObjectContextAdapter).ObjectContext.MetadataWorkspace; - var efEntityContainer = efModel.GetItems(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(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) diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.Designer.cs b/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.Designer.cs index d05c1dc1..39fc17eb 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.Designer.cs +++ b/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.Designer.cs @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 @@ -69,6 +69,15 @@ internal static string DataModificationMustBeCUD { } } + /// + /// Looks up a localized string similar to Could not find the correct DbContext instance for this EntityFrameworkApi. \r\n The Context name was '{0}' but the Container expects '{1}'.. + /// + internal static string DbContextCouldNotBeFoundException { + get { + return ResourceManager.GetString("DbContextCouldNotBeFoundException", resourceCulture); + } + } + /// /// Looks up a localized string similar to Need 'LineString type', while input is {0}.. /// @@ -87,6 +96,15 @@ internal static string InvalidPointGeographyType { } } + /// + /// Looks up a localized string similar to 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}'.. + /// + internal static string MultipleDbContextsExpectedException { + get { + return ResourceManager.GetString("MultipleDbContextsExpectedException", resourceCulture); + } + } + /// /// Looks up a localized string similar to The precondition check for request {0} on resource {1} is failed.. /// diff --git a/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.resx b/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.resx index dd1d9dd8..e328fed3 100644 --- a/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.resx +++ b/src/Microsoft.Restier.Providers.EntityFramework/Properties/Resources.resx @@ -1,4 +1,4 @@ - +