在使用代码之后更改实体框架数据库模式映射 [英] Change Entity Framework database schema map after using code first

查看:90
本文介绍了在使用代码之后更改实体框架数据库模式映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用EF和Code First构建了我的博客。



EF正在使用[DBO]模式针对本地SQL Express实例运行。



现在我想发布博客,而我已经完成了以下:


  1. 从SQL Express创建表和所有对象的脚本,并将[dbo]从我的服务器更改为[管理员]模式。

  2. 将脚本反对服务器。没有问题,所有的对象都创建了一个填充好的。

  3. 我已经修改了Webconfig,并添加了我的BlogContext连接字符串来指向服务器不是本地的sql express。

  4. 发布网站

我得到的错误是:无效的对象名称'dbo.Articles'。 - 文章是我的实体之一。它驻留在我的sql服务器上, [管理员]。文章



据我所知,EF仍然认为im使用DBO模式。虽然我已经添加了连接字符串来指向管理员用户。



如何更改EF认为应该使用的模式?

解决方案

如果没有通过数据注释或流畅的API明确配置模式,EF将使用 dbo 模式。

  [Table(MyTable,MySchema)] 
public class MyEntity
{

}

  modelBuidler.Entity< MyEntity>()。ToTable(MyTable,MySchema); 


I've finished building my blog using EF and Code First.

EF was running against my local SQL Express instance, with [DBO] schema.

Now i want to publish the blog, and i have done the following :

  1. Generetade the scripts for the tables and all objects from SQL Express and change [dbo] to my [administrator] schema from my server.
  2. Ran the scripts against the server. No issues, all objects were created an populated just fine.
  3. I have modified Webconfig and added my BlogContext connection string to point to the server not local sql express.
  4. Published the site.

The error i am getting is : Invalid object name 'dbo.Articles'. - where Articles is one of my entities. It resides on my sql server, [Administrator].Articles.

As far as i can tell EF still thinks im using the DBO schema. Although i have added the connection string to point to administrator user.

How can i change the schema that EF thinks it should use?

解决方案

EF will use dbo schema if you didn't configure the schema explicitly through data annotations or fluent API.

[Table("MyTable", "MySchema")]
public class MyEntity
{

}

Or

modelBuidler.Entity<MyEntity>().ToTable("MyTable", "MySchema");

这篇关于在使用代码之后更改实体框架数据库模式映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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