I am trying to create a constructor for my Context class from an ObjectContext. I want to do this because I am using DevExpress xaf which makes it easy to get hold of the ObjectContext from inside a view.
The MSDN help is here but it does not include an example
I have tried
public MyAppDbContext(ObjectContext objectContext) :
base(objectContext.Connection.ConnectionString) { }
however when I try to use a context created this way I get an error message
System.Data.Entity.Core.MetadataException was unhandled by user code
HResult=-2146232007
Message=At least one of the input paths is not valid because either it is too long or it has incorrect format.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.NormalizeFilePaths(String path)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.SplitPaths(String paths)
at System.Data.Entity.Core.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetArtifactLoader(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace()
at System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection()
at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection)
at System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommand(TransactionalBehavior transactionalBehavior, String sql, Object[] parameters)
at System.Data.Entity.Database.ExecuteSqlCommand(TransactionalBehavior transactionalBehavior, String sql, Object[] parameters)
at System.Data.Entity.Database.ExecuteSqlCommand(String sql, Object[] parameters)
at MyApp.Module.Controllers.SchedDataWork.ClearAllAppointmentsAndResources(MyAppDbContext connect) in c:\Users\kirsten\Documents\jtworkflow\MyApp.Module\Controllers\SchedDataWork.cs:line 86
at MyApp.Module.Controllers.SchedDataWork.ImportAppointmnentsFromTasks(IObjectSpace objectSpace) in c:\Users\kirsten\Documents\jtworkflow\MyApp.Module\Controllers\SchedDataWork.cs:line 57
at MyApp.Module.Win.Controllers.ListViewController.actImportAppointments_Execute(Object sender, SimpleActionExecuteEventArgs e) in c:\Users\kirsten\Documents\jtworkflow\MyApp.Module.Win\Controllers\ListViewController.cs:line 127
at DevExpress.ExpressApp.Actions.SimpleAction.RaiseExecute(ActionBaseEventArgs eventArgs)
at DevExpress.ExpressApp.Actions.ActionBase.ExecuteCore(Delegate handler, ActionBaseEventArgs eventArgs)
InnerException: System.NotSupportedException
HResult=-2146233067
Message=The given path's format is not supported.
Source=mscorlib
StackTrace:
at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.NormalizeFilePaths(String path)
InnerException:
I am able to create my context successfully using the following methods.
public class MyAppDbContext : DbContext {
public MyAppDbContext(String connectionString)
: base(connectionString) {
Database.SetInitializer(new MyAppDbInitializer());
}
public MyAppDbContext(DbConnection connection)
: base(connection, false) {
Database.SetInitializer(new MyAppDbInitializer());
}
public MyAppDbContext() // used for migrations
: base("name=ApplicationDatabase")
{
Database.SetInitializer(new MyAppDbInitializer());
}
}
If you want to use an existing ObjectContext
, the proper constructor is
public MyAppDbContext(ObjectContext objectContext)
: base(objectContext, false)
{ }
where the second argument in the base constructor is true
if you want the ObjectContext
to be disposed with the DbContext
.
https://msdn.microsoft.com/en-us/library/dn220058(v=vs.113).aspx