【发布时间】: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