简体   繁体   中英

Entity Framework 6 seems to be ignoring HasDefaultSchema

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM