我想列举一下新闻部分的一些数据。我有两张桌子。新闻与NewsCategory
这是我的模型类
public class News
{
public int NewsId { get; set; }
public string Name { get; set; }
public int NewsCategoryId { get; set; }
public virtual NewsCategory NewsCategory { get; set; }
}
public class NewsCategory
{
public int NewsCategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual List<News> News { get; set; }
}
public class NewsDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
}
public DbSet<News> News { get; set; }
public DbSet<NewsCategory> NewsCategory { get; set; }
}这也在起作用,当我在我的控制器中完成数据时,除了一件事。当我整理我的新闻时,我没有提到我的分类。
我的控制器代码:
var news = _db.News.ToList();这一产出如下:
[
{
"NewsId": 1,
"Name": "ghdfgd",
"NewsCategoryId": 1,
"NewsCategory": null
},
{
"NewsId": 2,
"Name": "gdfgdf",
"NewsCategoryId": 1,
"NewsCategory": null
}
]如您所见,NewsCategory是空的。虽然情况并非如此:)
我遗漏了什么?
发布于 2015-03-11 19:47:17
https://stackoverflow.com/questions/28995782
复制相似问题