Entity Framework 6 seems to be ignoring HasDefaultSchema

c# entity-framework entity-framework-6 schema sql-server

Question

I am working on a site where we'd like schema to be the differentiator between projects and in doing so, securing things up between projects.

I have had some luck with this using the HasDefaultSchema method in OnModelCreating in my data context, but also having to make my DbContext implement IDbModelCacheKeyProvider and implementing the CacheKey property. It has worked before. Unfortunately, this solution seems to be inconsistent and I am currently having a problem where I am trying to update my model but getting the following error when running Update-Database:

The specified schema name "dbo" either does not exist or you do not have permission to use it.

The connection string has a user that only has access to my xma schema, so this error makes sense if I had changed the schema, but you can see in the following code, I haven't:

public class DataContext : DbContext, DbModelCacheKeyProvider
{
    public DataContext()
        //: base("name=DataContext")
        : base("DataContext")
    {
    }

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

        base.OnModelCreating(modelBuilder);
    }

    public virtual DbSet<Article> Articles { get; set; }
    public virtual DbSet<Author> Authors { get; set; }
    ...

    public string CacheKey
    {
        get { return Utility.SchemaPrefix ?? "xma"; }
    }
}

This problem occurs with other team members too, so any help would be much appreciated.

Thanks

Edit: I forgot to mention, this appears to be a problem on a clean database.

1
0
2/23/2016 12:35:13 PM

Accepted Answer

I had removed the pending changes to the model, and was able to run update-database -script as advised by Steve, turns out that the initial migrations are based on the dbo schema so changed to a more privileged user and was able to continue with recreating the database. My mistake unfortunately.

0
2/24/2016 4:52:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow