C# 我可以更改实体框架 4.3 代码优先中的默认架构名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9562883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:54:05  来源:igfitidea点击:

Can I change the default schema name in entity framework 4.3 code-first?

c#sqlentity-frameworkentity-framework-4ef-code-first

提问by Chev

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.

目前,我正在将我的应用程序部署到共享托管环境,并且代码优先迁移一直运行良好,除了一个小问题。每次我想推送站点时,我都必须使用“Update-Database -script”选项,因为我必须在每个表名之前加上,[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.

如果我登录到我的共享主机并创建一个数据库,那么我必须创建一个用户。如果我将该用户命名为 admin,则以 admin 身份登录时代码优先创建的表类似于“[admin].[BlogPosts]”。当应用程序运行时,所有表都被创建,但我得到一个 EF 异常,因为它说“[dbo].[BlogPosts]”是无效的。如果我将表的架构名称重命名为“[dbo]”而不是“[admin]”来修复它。

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.

为了解决这个问题,我必须生成一个要手动执行的迁移脚本,并在所有表名前添加“[dbo]”,因为该脚本仅通过名称而不是架构和名称来引用表。

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.

有没有简单的方法来解决这个问题?如果我所要做的就是发布应用程序并且一切正常,那就太好了。如果不是因为模式名称的差异,那将是一键部署,一切都会很美好。

采纳答案by Eranga

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

您可以使用该ToTable方法来指定架构名称。如果不指定架构名称,EF 将按照惯例使用dbo.

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

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

回答by Bill

For database first implementations, its easy. Open the edmx file, right click-> Properties and set the default database schema.

对于数据库优先实现,它很容易。打开 edmx 文件,右键单击-> 属性并设置默认数据库架构。

For code first, this article seems most promising. http://devproconnections.com/entity-framework/working-schema-names-entity-framework-code-first-design

对于代码优先,这篇文章似乎最有前途。 http://devproconnections.com/entity-framework/working-schema-names-entity-framework-code-first-design

回答by ferhrosa

For those using Entity Framework 6, just use the HasDefaultSchemamethod:

对于使用 Entity Framework 6 的用户,只需使用以下HasDefaultSchema方法:

public class Contexto : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("MyDefaultDbSchema");
    }
}

回答by petrosmm

I would like to add since this is for C#, I have written one below for VB

我想补充一下,因为这是针对 C# 的,我在下面为 VB 写了一个

Public Class ClientDbContext
Inherits DbContext
Public Property Clients As DbSet(Of Client)

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    modelBuilder.HasDefaultSchema("dbo")
End Sub
End Class

回答by BehrouzMoslem

In EF Code first, by default, everything is set up based on user access with a managerial access "DBO-Schema" in the SQL Server. But if a specific user is defined to work with a database that is common in shared hosting, then there will no longer be Dbo management access. This time the names of our tables are dbo.tableName, for example, someUser.tableName, and inaccuracy of this point makes it impossible to run the program. To modify and explicitly assign a user connected to a database. If you use metadata, the following method should be used:

EF Code first 中,默认情况下,一切都是基于用户访问权限设置的,在 SQL Server 中具有管理访问权限“ DBO-Schema”。但如果特定用户被定义为使用共享主机中常见的数据库,则将不再有 Dbo 管理访问权限。这次我们的表名是dbo.tableName,比如someUser.tableName,这点不准确导致程序无法运行。修改并显式分配连接到数据库的用户。如果使用元数据,则应使用以下方法

[Table("MyTableName", Schema="MySchemaName")]
public class MyClassName
{
 //Other Lines...
}

Or(Whether or not Fluent APIis customizable as follows:)

或者(是否Fluent API可自定义如下:)

modelBuilder.Entity<Blog>().ToTable("MyTableName", schemaName:"MySchemaName");

Notice the following: enter image description here

请注意以下几点: 在此处输入图片说明

a good reference for study: http://www.c-sharpcorner.com/article/fluent-api-in-code-first-approach/

一个很好的学习参考:http: //www.c-sharpcorner.com/article/fluent-api-in-code-first-approach/