列表表导致“在数据库中拒绝CREATE TABLE权限”ASP.NET – MVC4
我正在使用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
控制器:
namespace TestDBCon.Controllers { public class HomeController : Controller { private DataDbContext db = new DataDbContext(); public ActionResult Index() { return View(db.AccControls.ToList()); } } }
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 AccControls { get; set; } } }
Web.Config中:
我不想创建一张桌子? 我只是想列出结果,所以我非常困惑。 它必须与MVC有关吗?
任何帮助将不胜感激!
谢谢
您的web config
指向正确的数据库?
凭证是否正确?
如果要使用MVC4 WebSecutiy()
来处理用户的身份validation和授权,Entity Framework将在数据库中创建表。 这可能是抛出exception。
在这种情况下,您无法创建成员资格提供程序所需的表,您需要将其从系统中排除。 在这看到这个。 或者,创建一个新的MVC4项目并选择空模板,然后输入您需要的位。
我知道它已经老了,但由于我遇到了同样的问题,我花了一段时间才找到解决方案……我决定分享这些信息。 所以我必须做两件事才能摆脱这个问题,第一件事是禁用迁移:
# Migrations/Configuration.cs internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = false; } }
然而这还不够,我还必须确保播种机没有运行。 你可以用这段额外的代码取消它:
#Global.asax.cs protected void Application_Start() { AreaRegistration.RegisterAllAreas(); Database.SetInitializer(null); Database.SetInitializer(null); ... }
最后,我现在可以使用LINQ进行SELECT,并且只具有READ访问权限
编辑
根据劳伦斯的建议,最好直接在DB Context Constructor中使用它。 感谢您的提示,我更新了我的代码。 它看起来像这样:
public partial class MyDb1 : DbContext { public MyDb1() : base("name=MyDb1Connection") { Database.SetInitializer(null); } ... }
- 如何使用MVC.Net建模绑定“List ”列表
- Sitecore – ControllerRendering抱怨控制器“未找到或未实现IController”。
- asp.net core 2.0中System.Web.Hosting.HostingEnvironment.RegisterObject的替代方案
- 未定义或导入预定义类型“System.Object”.net 4.6
- ASP.NET MVC Route:绕过路径的静态文件处理程序
- ASP.Net MVC:如何读取我的自定义声明值
- 如何推动自定义控制器工厂ASP.Net MVC
- 使用angular post将两个参数传递给WEB API调用
- 无法在Web API中正确获取文件扩展名