我在ASP.Net Web窗体应用程序中使用EF 5和Code First,并创建了一个名为“Compleate”的主上下文,当我对它进行LINQ调用时,一切正常。因为我运行的服务器没有很多资源,所以我每次需要查询数据库时都会使用较小的上下文来限制调用的数据范围。问题是,当我针对较小的上下文进行LINQ调用时,我总是得到错误
值不能为空。参数名称:外部
我一直试图解决这个问题几天,我迷路了。对我在较小的环境中做错的任何帮助都会很棒。
Compleate.cs(运行并运行代码优先迁移的上下文):
using FFInfo.DAL.Tables;
using System.Data.Entity;
namespace FFInfo.DAL
{
public class Compleate : DbContext
{
public Compleate() : base("FFInfoDB") { }
//General Tables
public DbSet<File> Files { get; set; }
public DbSet<Culture> Cultures { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<Navigation> Navigation { get; set; }
//Locale Tables
public DbSet<Locale_Section> Locale_Sections { get; set; }
}
}
Base.cs(所有较小的上下文都继承自此):
using System.Data.Entity;
namespace FFInfo.DAL
{
public class Base<TContext> : DbContext where TContext : DbContext
{
static Base()
{
Database.SetInitializer<TContext>(null);
}
protected Base() : base("FFInfoDB") { }
}
}
SiteNavigation.cs(我试图读取的较小的上下文):
using FFInfo.DAL.Tables;
using System.Data.Entity;
namespace FFInfo.DAL
{
public class SiteNavigation : Base<SiteNavigation>
{
public DbSet<Navigation> Navigation { get; private set; }
public DbSet<Section> Sections { get; private set; }
public DbSet<Locale_Section> SectionTranslations { get; private set; }
}
}
LINQ CODE:
protected void Page_Load(object sender, EventArgs e)
{
rpBooks.DataSource = PopulateNav("Book", 1);
rpBooks.DataBind();
}
protected object PopulateNav(string Category, byte Culture)
{
using (var db = new SiteNavigation())
{
return (from n in db.Navigation
join st in db.SectionTranslations on n.SectionID equals st.Section.SectionID
where n.Category == Category && st.CultureID == Culture
select new
{
LinkAddress = st.Section.Type + "/" + st.Section.RouteName,
st.Title
}).ToList();
}
}
答案 0 :(得分:0)
using FFInfo.DAL.Tables;
using System.Data.Entity;
namespace FFInfo.DAL
{
public class SiteNavigation : Base<SiteNavigation>
{
public DbSet<Navigation> Navigation { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<Locale_Section> SectionTranslations { get; set; }
}
}