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)
| 归档时间: |
|
| 查看次数: |
13407 次 |
| 最近记录: |