2016-06-10 161 views
1

實體框架Core未返回任何結果。我一直在尋找近和遠。我發現一些教程指出一件事,另一些則說另一件事。以下是我迄今爲止:EF核心+ MVC 6 + .NET核心RC2 - EF未返回結果

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; 
    } 
} 

這是所有返回空括號當我加載頁面,而不是買家的列表。但是該表中有超過1000行(dbo.DealerCustomer)。我知道我有兩個地方添加數據庫連接字符串,但教程保持顯示這樣做的兩種方式,當我只在startup.cs中執行它時,我得到有關_context的錯誤。我可以在一切看起來很美,現在我只想要一個很好的連接,所以我有一個開始工作的起點。

回答