EF代码首先,如何使用不同的模式注册同一个表名? [英] EF Code first, how to register same table name with different schema?

查看:141
本文介绍了EF代码首先,如何使用不同的模式注册同一个表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Entity Framework,Code First,而在我们的数据库中,我们有几个具有相同名称但不同Schema的表。

We are using Entity Framework, Code First and in our database we have several tables that have the same name but in different Schemas.

我也把模型在两个不同的命名空间中。

I have also put the models in two different namespaces.

如何在DbContext类中注册这些表?

How i can register these tables in my DbContext class?

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Data.Schema1.Contact>().ToTable("Contact", "schema1");
    modelBuilder.Entity<Data.Schema2.Contact>().ToTable("Contact", "schema2");
}

感谢您的帮助提前!

推荐答案

您的类必须具有不同的名称,或者您必须为每个模式使用单独的上下文。

Your classes must have different name or you must use separate context for every schema.

其原因是内部使用的EDM模型。即使您使用的是代码优先,它仍然在后面创建EDM模型,并且必须遵循其所有限制以及POCO类如何与CSDL模型中定义的实体匹配的方式。来自EDM的实体和POCO类与类名称匹配(无命名空间)。因为在同一上下文中映射的每个类名必须是唯一的,并且不同的命名空间不会使其具有不同的类名。

The reason for this is EDM model used internally. Even if you are using code-first it still creates EDM model on behind and it must follow all its restrictions and the way how POCO classes are matched to entities defined in CSDL model. Entities from EDM are and POCO classes are matched by class name (without namespaces). Because of that each class name mapped in the same context must be unique and different namespace doesn't make it different class name.

这篇关于EF代码首先,如何使用不同的模式注册同一个表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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