简体   繁体   English

如何从ObjectContext构造DbContext?

[英]How do I construct a DbContext from an ObjectContext?

I am trying to create a constructor for my Context class from an ObjectContext. 我试图从ObjectContext为我的Context类创建一个构造函数。 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. 我想这样做是因为我正在使用DevExpress xaf,它使从视图内部轻松获取ObjectContext变得容易。

The MSDN help is here but it does not include an example MSDN帮助在这里,但不包含示例

I have tried 我努力了

public JobTalkDbContext(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 JobTalk.Module.Controllers.SchedDataWork.ClearAllAppointmentsAndResources(JobTalkDbContext connect) in c:\Users\kirsten\Documents\jtworkflow\JobTalk.Module\Controllers\SchedDataWork.cs:line 86
   at JobTalk.Module.Controllers.SchedDataWork.ImportAppointmnentsFromTasks(IObjectSpace objectSpace) in c:\Users\kirsten\Documents\jtworkflow\JobTalk.Module\Controllers\SchedDataWork.cs:line 57
   at JobTalk.Module.Win.Controllers.ListViewController.actImportAppointments_Execute(Object sender, SimpleActionExecuteEventArgs e) in c:\Users\kirsten\Documents\jtworkflow\JobTalk.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 JobTalkDbContext : DbContext {
    public JobTalkDbContext(String connectionString)
        : base(connectionString) {
            Database.SetInitializer(new JobTalkDbInitializer());
    }

    public JobTalkDbContext(DbConnection connection)
        : base(connection, false) {
            Database.SetInitializer(new JobTalkDbInitializer());
    }

    public JobTalkDbContext()  // used for migrations
        : base("name=ApplicationDatabase")
    {
        Database.SetInitializer(new JobTalkDbInitializer());
    }
}

If you want to use an existing ObjectContext , the proper constructor is 如果要使用现有的ObjectContext ,则适当的构造函数是

public JobTalkDbContext(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 . 如果希望将ObjectContextDbContext一起放置,则基本构造函数中的第二个参数为true

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM