我可以更改实体框架4.3中的默认模式名吗?

时间:2022-02-22 06:53:59

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]”。(言论)”是无效的。如果我将表的模式名称重命名为“[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.

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

5 个解决方案

#1


32  

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.

您可以使用ToTable方法来指定模式名。如果不指定模式名,EF将按照约定使用dbo。

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

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

#2


121  

For those using Entity Framework 6, just use the HasDefaultSchema method:

对于使用实体框架6的用户,只需使用HasDefaultSchema方法:

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

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

#3


6  

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

#4


4  

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

#5


2  

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代码中,默认情况下,一切都是基于在SQL服务器中具有管理访问“DBO-Schema”的用户访问进行设置的。但是,如果一个特定的用户被定义为使用共享主机中常见的数据库,那么Dbo管理访问将不再存在。这一次我们的表的名称是dbo。表名,例如,someUser。tableName和这一点的不准确性使程序无法运行。修改并显式地分配连接到数据库的用户。如果您使用元数据,应该使用以下方法:

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

Or (Whether or not Fluent API is customizable as follows:)

或(无论是否流畅的API可定制如下:)

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

Notice the following: 我可以更改实体框架4.3中的默认模式名吗?

请注意以下几点:

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/

#1


32  

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.

您可以使用ToTable方法来指定模式名。如果不指定模式名,EF将按照约定使用dbo。

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

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

#2


121  

For those using Entity Framework 6, just use the HasDefaultSchema method:

对于使用实体框架6的用户,只需使用HasDefaultSchema方法:

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

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

#3


6  

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

#4


4  

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

#5


2  

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代码中,默认情况下,一切都是基于在SQL服务器中具有管理访问“DBO-Schema”的用户访问进行设置的。但是,如果一个特定的用户被定义为使用共享主机中常见的数据库,那么Dbo管理访问将不再存在。这一次我们的表的名称是dbo。表名,例如,someUser。tableName和这一点的不准确性使程序无法运行。修改并显式地分配连接到数据库的用户。如果您使用元数据,应该使用以下方法:

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

Or (Whether or not Fluent API is customizable as follows:)

或(无论是否流畅的API可定制如下:)

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

Notice the following: 我可以更改实体框架4.3中的默认模式名吗?

请注意以下几点:

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/