简体   繁体   English

如何使用实体框架核心1.0从ASP.MVC核心的数据库中读取记录

[英]How to Read Record From Database in ASP.MVC Core using Entity Framework Core 1.0

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; 我正在使用.NET Core 1.0开发简单的MVC 6应用程序,并尝试使用Entity Framework Core 1.0从数据库中读取数据,并在LINQ查询中从尝试读取表的位置获取以下错误; The LINQ query is in HomeController class LINQ查询在HomeController类中

Error 错误

An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'TestModel'.

Model Class 型号类别

[Table("TestTable")]
public class TestModel
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}

TestDbContext 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 project.json

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"System.ComponentModel.Annotations": "4.1.0"

appsettings.json 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 (trying to read table here!) 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();
    }

Not sure what I missing here! 不知道我在这里错过了什么!

My Fault found error, was giving wrong table name in override OnModelCreating 我的错误发现错误,在覆盖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");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Entity Framework Core从数据库记录的最大和最小 - Max and min record from database using Entity Framework Core 如何在 ASP.NET Core web 应用程序中使用 Entity Framework Core 从数据库中保存和检索图像? - How to save and retrieve images from database using Entity Framework Core in ASP.NET Core web application? Entity Framework Core 和 ASP.NET Core MVC 中的 Inheritance 数据库 - Inheritance Database in Entity Framework Core and ASP.NET Core MVC 如何将 email 列表从 ASP.NET Core MVC 中的数据库实体框架传递给 MailboxAddress - How to pass email list to MailboxAddress from database Entity Framework in ASP.NET Core MVC 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop 如何使用 Entity Framework Core 从 sql 脚本创建数据库? - How to create a database from sql script using Entity Framework Core? 如何使用实体框架使用“核心”数据库和各个派生数据库来构造ASP.NET MVC应用程序? - How can I structure an ASP.NET MVC application with a “Core” database and individual derived databases using Entity Framework? ASP.NET Core MVC &amp; C#:使用实体框架将数据插入数据库 - ASP.NET Core MVC & C# : insert data into database using Entity Framework 实体框架核心将记录插入数据库 - Entity Framework Core insert record into database ASP.NET 核心 - 如何使用实体框架更新现有记录并同时插入新记录 - ASP.NET Core - How to update existing record and insert a new one at the same time using Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM