Reputation: 5075
I am working on simple MVC 6 app with .NET Core 1.0 and trying to read data from database using Entity Framework Core 1.0 and getting following error in my LINQ query from where I am trying to read table; The LINQ query is in HomeController class
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; }
}
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");
}
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UCASAppDatabase")));
services.AddMvc();
}
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"System.ComponentModel.Annotations": "4.1.0"
{
"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"
}
}
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();
}
Not sure what I missing here!
Upvotes: 0
Views: 2048
Reputation: 5075
My Fault found error, was giving wrong table name in override OnModelCreating
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("TestTable");
}
}
Upvotes: 1