【问题标题】:EF Core + MVC 6 + .NET Core RC2 - EF not returning resultsEF Core + MVC 6 + .NET Core RC2 - EF 不返回结果
【发布时间】:2016-10-10 21:49:49
【问题描述】:

Entity Framework Core 没有返回任何结果。我一直在寻找近处和远方。我发现一些教程说一件事,而其他教程说另一件事。这是我目前所拥有的:

Buyer.cs

[Table("DealerCustomer", Schema = "dbo")]
public class Buyer
{
    [Key]
    public int DealerCustomerKey { get; set; }
    public int DealerId { get; set; }
}

BuyerContext.cs

public class BuyerContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlServer("db connection string here");
    }
    public DbSet<Buyer> Buyers { get; set; }
}

Startup.cs > ConfigureServices 函数

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddDbContext<BuyerContext>(options =>
        options.UseSqlServer("db connection string here");

    services.AddMvc();
}

现在我正在尝试从我的 BuyerController.cs 加载买家数据:

[Route("api/[controller]")]
public class BuyersController : Controller
{
    private BuyerContext _context;

    public BuyersController(BuyerContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<Buyer> Get()
    {
        System.Diagnostics.Debug.WriteLine("getting buyers");
        System.Diagnostics.Debug.WriteLine(_context.Buyers);
        return _context.Buyers;
    }
}

当我加载页面时,这都是返回空括号,而不是买家列表。但是,该表 (dbo.DealerCustomer) 中有超过 1000 行。我知道我有两个地方添加了 db 连接字符串,但是教程一直显示这两种方法,当我只在 startup.cs 中这样做时,我收到了关于 _context 的错误。以后我可以让一切看起来很漂亮,现在我只想要一个良好的连接,这样我就有了一个工作的起点。

【问题讨论】:

    标签: asp.net-mvc entity-framework-core .net-core-rc2


    【解决方案1】:

    我发现有一个超时,因为其中一个十进制字段返回 null。

    EF Core Timing out on null response

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2017-06-13
      • 2022-07-15
      相关资源
      最近更新 更多