如何从ObjectContext构造DbContext?

时间:2015-05-11 22:10:01

标签: c# entity-framework ef-code-first entity-framework-6 xaf

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

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

我试过了

public JobTalkDbContext(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 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: 

我可以使用以下方法成功创建我的上下文。

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

1 个答案:

答案 0 :(得分:1)

如果要使用现有的<!--Body--> <div id="bodyContainer"> <div id="indexSpotlightContainer"> SLIDER </div> <div class="indexBlockDivider"></div> <div id="indexBlockContainer1"> <div class="indexInfoBlock"> BLOCK1 </div> </div> <div class="indexBlockDivider"></div> <div id="indexBlockContainer2"> BLOCK2 </div> <div class="indexBlockDivider"></div> <div id="indexBlockContainer3"> <div class="indexSpotContainer1"> BLOCK3 </div> <div class="indexSpotContainer2"> BLOCK4 </div> <div class="indexSpotContainer3"> BLOCK5 </div> </div> </div> <!--Body--> ,正确的构造函数是

ObjectContext

如果您希望public JobTalkDbContext(ObjectContext objectContext) : base(objectContext, false) { } true一起使用,那么基础构造函数中的第二个参数为ObjectContext

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