EF CTP5 - 跨多个组件的上下文继承 [英] EF CTP5 - Context Inheritance Across Multiple Assemblies

查看:132
本文介绍了EF CTP5 - 跨多个组件的上下文继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个程序集,每个程序集都有模型和模型上下文。

I have two assemblies, each with models and a model context.

第一个程序集模型上下文派生自DbContext。

The first assembly model context is derived from DbContext.

第二个装配体模型上下文是从第一个装配模型上下文导出的。

The second assembly model context is derived from the first assembly model context.

除了数据库生成失败,在生成数据库时考虑。

This works, except the database generation fails because the first assembly models aren't considered when generating the database.

有没有办法确保在数据库生成过程中第一个装配体模型被正确考虑?

Is there a way to ensure that the first assembly models are properly considered during database generation?

推荐答案

我通过将其他程序集的元数据加载到上下文的构造函数中的基础ObjectContext的MetadataWorkspace中来解决这个问题:

I solved this by loading the other assembly's metadata into the underlying ObjectContext's MetadataWorkspace within the context's constructor:

namespace MyNamespace{
    public class MyContext : DbContext {

        public ObjectContext ObjectContext { 
            get { return ((IObjectContextAdapter)this).ObjectContext; } 
        }

        public MyContext() : base() {
            this.ObjectContext.MetadataWorkspace.LoadFromAssembly(
               System.Reflection.Assembly.GetAssembly(typeof(MyNamespace.MyContext))
            );
        }

    }
}

这篇关于EF CTP5 - 跨多个组件的上下文继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆