Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First ignoring specific schema

Using Entity Framework 4.3.1 CodeFirst and having no luck getting the migrations or scripts to respect the schema that I want my tables to end up in.

It seems the default behavior (the one that I'm seeing regardless of what I do) is to omit the schema completely from the SQL that actually runs causes tables to be created in the default schema for the user running the migration or script.

My DBAs are telling me that they cannot change my default schema due to the fact that I'm part of an AD group and not a local user, so changing the default schema for the user running (an often recommended workaround) the script is not an option at all.

I've tried using the annotations like this:

[Table("MyTable", Schema = "dbo")]
public class MyTable
{
    public int Id { get; set; }

    public string MyProp1 { get; set; }

    public string MyProp2 { get; set; }
}

And I've also tried using the fluent variant of the same thing:

modelBuilder.Entity<YourType>().ToTable("MyTable", "dbo");

The resultant script (and migrations) ignore the fact that I tried to specify a schema. The script looks like this:

CREATE TABLE [MyTable] (
    [Id] [int] NOT NULL IDENTITY,
    [MyProp1] [nvarchar](max),
    [MyProp2] [nvarchar](max),
    CONSTRAINT [PK_MyTable] PRIMARY KEY ([Id])
)

When there should be a [dbo] tucked in there like this:

CREATE TABLE [dbo].[MyTable] (
    [Id] [int] NOT NULL IDENTITY,
    [MyProp1] [nvarchar](max),
    [MyProp2] [nvarchar](max),
    CONSTRAINT [PK_MyTable] PRIMARY KEY ([Id])
)

Has anyone else had luck in getting Entity Framework to respect the schema? This behavior pretty much kills our ability to use codefirst at all in our enterprise environment.

Reminder: Changing my user to have a different default schema is not an option at all.

like image 265
Bob Bland Avatar asked May 21 '12 14:05

Bob Bland


People also ask

How do I change the schema in Entity Framework?

There are 2 ways to change the schema, either by applying the TableAttribute or by implementing the interface IEntityTypeConfiguration<TEntity> . The first option won't help us because the schema is hard-coded. The second option gives us the ability to provide the schema from DbContext to the EF model configuration.

How do I create a schema in Entity Framework Code First?

As with any code-first schema customization, you can do this by using the entity classes' attributes or through the DbModelBuilder API. With data annotations, you can use the optional second parameter of the Table attribute to specify the schema name. The code in Figure 3 implements this change in the model.


1 Answers

As my comment seems to have answered the quandary, I am recreating it as an answer.

It seems that because the SQL Server provider uses "dbo" as the default schema, it will not explicitly add "dbo" to the TSQL that creates the tables even if you specify that in your configuration.

This answers the basic problem. But now I am curious if dbo is the default, do you (Bob) still have a reason to specify it? It doesn't hurt to have it in the configuration if you just want the default to be obvious to someone reading the code. But is the behavior creating another side-effect?

ADDED: Aha! FIXED IN EF5! :) (I just updated my test project to use EF5RC (against .NET 4.0) and I got "dbo" explicitly in the TSQL for create table.)

like image 63
Julie Lerman Avatar answered Oct 11 '22 10:10

Julie Lerman