列表导致"在数据库中拒绝CREATE TABLE权限"ASP.NET - MVC4

11 c# asp.net-mvc database-connection database-permissions asp.net-mvc-4

我正在使用ASP.NET MVC 4 - c#连接到实时数据库,并列出结果,但是当我去查看页面时它返回以下错误:

CREATE TABLE permission denied in database 'DatabaseName'.

Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it
originated in the code.



Exception Details: System.Data.SqlClient.SqlException: CREATE TABLE
permission denied in database 'DatabaseName'.

Source Error: 


Line 16:         public ActionResult Index()
Line 17:         {
Line 18:             return View(db.AccControls.ToList());
Line 19:         }
Line 20
Run Code Online (Sandbox Code Playgroud)

控制器:

namespace TestDBCon.Controllers
{
    public class HomeController : Controller
    {
        private DataDbContext db = new DataDbContext();

        public ActionResult Index()
        {
            return View(db.AccControls.ToList());
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

AccControl.cs(型号)

namespace TestDBCon.Models
{
    public class AccControl
    {
        public int ID { get; set; }
        public int ControlCode { get; set; }
        public string Nominal { get; set; }
        public string CostCentre { get; set; }
        public string Department { get; set; }
    }

    public class DataDbContext : DbContext
    {
        public DbSet<AccControl> AccControls { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Web.Config中:

<add name="DataDbContext" connectionString="Data Source=***;initial catalog=***;integrated security=True;" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)

我不打算创建一张桌子?我只是想列出结果,所以我非常困惑.它必须与MVC有关吗?

任何帮助将不胜感激!

谢谢

ghi*_*ing 18

我知道它已经老了,但由于我遇到了同样的问题,我花了一段时间才找到解决方案......我决定分享信息.所以我不得不做两件事来解决这个问题,第一件事是禁用迁移:

# Migrations/Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<IntranetApplication.Models.MyDb1>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这还不够,我还必须确保播种机没有运行.你可以用这段额外的代码取消它:

#Global.asax.cs
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    Database.SetInitializer<Models.MyDb1>(null);
    Database.SetInitializer<Models.MyDb2>(null);

    ...
}
Run Code Online (Sandbox Code Playgroud)

最后,我现在可以使用LINQ进行SELECT,并且只有READ访问

编辑
根据Lawrence的建议,最好直接在DB Context Constructor中使用它.感谢您的提示,我更新了我的代码.它看起来像这样:

public partial class MyDb1 : DbContext
{
    public MyDb1()
        : base("name=MyDb1Connection")
    {
        Database.SetInitializer<Models.MyDb1>(null);
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这对于只读的实体框架至关重要,应该被标记为答案.如果没有这个,您无法在没有EF尝试迁移的情况下更改超时.但是,它不需要在Application_Start中完成.如果可以在modelContext的构造函数中完成. (3认同)

CR4*_*G14 4

web config指向正确的数据库吗?

凭证是否正确?

如果您要使用实体框架来MVC4 WebSecutiy()处理用户的身份验证和授权,实体框架将在数据库中创建表。这可能会引发异常。

在这种情况下,如果您无法创建成员资格提供程序所需的表,则需要将其从系统中排除。请参阅此处。或者创建一个新的 MVC4 项目并选择空模板,然后放入您需要的部分。