【发布时间】:2016-11-15 22:45:41
【问题描述】:
我正在使用 .NET Core 1.0 开发简单的 MVC 6 应用程序,并尝试使用 Entity Framework Core 1.0 从数据库中读取数据,并在我尝试读取表的 LINQ 查询中出现以下错误; LINQ 查询在 HomeController 类中
错误
An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'TestModel'.
模型类
[Table("TestTable")]
public class TestModel
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
TestDbContext
public class TestDbContext: DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options): base(options)
{ }
public DbSet<TestModel> TestModels { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TestModel>().ToTable("TestModel");
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UCASAppDatabase")));
services.AddMvc();
}
project.json
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"System.ComponentModel.Annotations": "4.1.0"
appsettings.json
{
"ApplicationInsights": {
"InstrumentationKey": ""
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"UCASAppDatabase": "Data Source=mydb;Initial Catalog=UCAS-DB;Integrated Security=True;Persist Security Info=True"
}
}
HomeController(正在这里读取表格!)
public class HomeController : Controller
{
private readonly TestDbContext _context;
public HomeController(TestDbContext context)
{
this._context = context;
}
public IActionResult About()
{
var query = (from b in _context.TestModels
select b).ToList();
ViewData["Message"] = "Your application description page.";
return View();
}
不知道我在这里错过了什么!
【问题讨论】:
标签: c# asp.net-core-mvc sql-server-2014 entity-framework-core