简体   繁体   English

实体框架6似乎忽略了HasDefaultSchema

[英]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. 我有一些运气这个使用HasDefaultSchema的方法OnModelCreating在我的数据方面,也有让我DbContext执行IDbModelCacheKeyProvider贯彻CacheKey财产。 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 : 不幸的是,此解决方案似乎不一致,并且我目前在尝试更新模型时遇到问题,但是在运行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: 连接字符串的用户只能访问我的xma模式,因此,如果我更改了模式,则此错误是有道理的,但是您可以在以下代码中看到,但我没有:

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. 我已经删除了对模型的未决更改,并且能够按照Steve的建议运行update-database -script ,结果证明,初始迁移基于dbo模式,因此更改为特权更高的用户,并且能够继续重新创建数据库。 My mistake unfortunately. 不幸的是我的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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