如何从 ObjectContext 构建 DbContext?

How do I construct a DbContext from an ObjectContext?

我正在尝试从 ObjectContext 为我的上下文 class 创建一个构造函数。 我想这样做是因为我正在使用 DevExpress xaf,它可以很容易地从视图内部获取 ObjectContext。

MSDN 帮助是 here 但它不包含示例

我试过了

public MyAppDbContext(ObjectContext objectContext) :
       base(objectContext.Connection.ConnectionString) { }

然而,当我尝试使用以这种方式创建的上下文时,我收到一条错误消息

  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());
        }
    }
    

如果您想使用现有的 ObjectContext,正确的构造函数是

public MyAppDbContext(ObjectContext objectContext) 
    : base(objectContext, false)
{ }

如果您希望 ObjectContextDbContext 一起处理,则基本构造函数中的第二个参数是 true

https://msdn.microsoft.com/en-us/library/dn220058(v=vs.113).aspx