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 &gt;配置服务功能
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;
}
}
当我加载页面时,这都是返回空括号,而不是买家列表。但是该表中有超过1000行(dbo.DealerCustomer)。我知道我有两个地方添加数据库连接字符串,但教程保持显示两种方式,当我只在startup.cs中做到这一点时,我收到有关_context的错误。我可以让所有东西看起来都很晚,现在我只想要一个良好的连接,所以我有一个起点可以工作。