我可以更改实体框架4.3代码优先默认模式叫什么名字? [英] Can I change the default schema name in entity framework 4.3 code-first?

查看:156
本文介绍了我可以更改实体框架4.3代码优先默认模式叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我部署我的应用程序共享主机环境和代码先用迁移已除一小的打嗝伟大的工作。每次我要推的网站,我必须使用更新,数据库-script选项,因为我必须在前面加上每个表名 [DBO] 因为默认情况下共享主机创建一个默认架构名称是相同的名称数据库用户名。

Currently I am deploying my application to a shared hosting environment and code-first with migrations has been working great except for one minor hiccup. Everytime I want to push the site I have to use the "Update-Database -script" option because I have to prepend every table name with [dbo] because by default the shared host creates a default schema name that is the same name as the database username.

如果我登录到我的共享主机并创建一个数据库,然后我必须创建一个用户。如果我的名字是admin用户,则表代码首先创建身份登录时管理是这个样子[管理员]。[相关博客文章]。当应用程序运行所有的表都创建,但我得到一个EF例外,因为它说:[DBO]。[相关博客文章]是无效的。如果我重新命名表的架构名称以[DBO]而非[管理员]能解决它。

If I log into my shared host and create a database, I then have to create a user. If I name that user admin, then the tables code-first creates while logged in as admin look something like this "[admin].[BlogPosts]". When the application runs all the tables are created but I get an EF exception because it says "[dbo].[BlogPosts]" is invalid. If I rename the table's schema name to "[dbo]" instead of "[admin]" that fixes it.

要解决这个问题我必须生成一个迁移脚本要手动执行,并添加[DBO]中的所有表名的前面,因为脚本只能由他们的名字由自己的模式和他们的名字引用的表,不是。

To get around this I have to generate a migrations script to be executed manually and add "[dbo]" in front of all the table names because the script only references the tables by their name, not by their schema and their name.

有没有一种简单的方法来解决这个问题?这将是那么好,如果我不得不做的是发布应用程序,一切都只是工作。如果不是因为架构名称不一致这将是一个点击部署,一切都将是辉煌的。

Is there an easy way to get around this? It would be so nice if all I had to do was publish the application and everything just worked. If it wasn't for the schema name discrepancy it would be a one click deploy and everything would be glorious.

推荐答案

您可以使用 ToTable 方法指定架构名称。如果不指定架构名称,EF将按照约定使用 DBO

You could use the ToTable method to specify the schema name. If you do not specify the schema name, EF will by convention use dbo.

public class MyContext
{
    private string schemaName = "Foo";

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
    } 
}

这篇关于我可以更改实体框架4.3代码优先默认模式叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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