【问题标题】:How to Read Record From Database in ASP.MVC Core using Entity Framework Core 1.0如何使用 Entity Framework Core 1.0 从 ASP.MVC Core 中的数据库读取记录
【发布时间】: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


    【解决方案1】:

    我的错误发现错误,在覆盖 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");
        }
    }
    

    【讨论】:

    • 我认为 EF Core 数据注释不起作用,所有映射都将使用 fluent api 执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2016-05-14
    • 2021-02-11
    相关资源
    最近更新 更多