将模型对象映射到DataConext

时间:2014-01-08 22:44:52

标签: c# .net linq compiled-query linq.compiledquery

以下是我第一次尝试编译linq查询时尝试使用的代码。代码构建正常,站点将按预期运行,但是一旦程序调用编译方法,我就会收到错误:The type 'Monet.Models.ZipCodeTerritory' is not mapped as a Table.

我似乎无法在网上找到任何与此特定问题相关的内容。有人能告诉我将ZipCodeTerritory表映射到DataContext对象需要做什么吗?我正在使用实体框架,ZipCodeTerritory是此页面上使用的模型。

参数/构造

private static Func<DataContext, ZipCodeTerritory, List<ZipCodeTerritory>> SearchEffectiveDate;
private static DataContext _dbContext;
public ZipCodeValidatorCompiled()
{
    _dbContext = new DataContext(ConfigurationManager.AppSettings.Get("db"));
    SearchEffectiveDate = CompiledQuery.Compile((DataContext db, ZipCodeTerritory zipCode)
    =>
    from z in db.GetTable<ZipCodeTerritory>()
    where z.DrmTerrDesc.Equals(zipCode.DrmTerrDesc) &&
            z.IndDistrnId.Equals(zipCode.IndDistrnId) &&
            z.StateCode.Equals(zipCode.StateCode) &&
            (z.ZipCode.Equals(null) ? z.ZipCode.Equals(null) : z.ZipCode.Equals(zipCode.ZipCode))
    select z
    );
}

编译方法调用

List<ZipCodeTerritory> terrList = SearchEffectiveDate(_dbContext, zipCode); //Error thrown here

修改

我像这样更新了参数和构造函数。现在我尝试自己创建ZipCodeTerritory表的一个实例,然后在预编译的查询中使用它。但是,我在构造函数中的这一行收到完全相同的消息:ZipCode = _dbContext.GetTable<ZipCodeTerritory>();

参数/构造

private static DataContext _dbContext;
private static Table<ZipCodeTerritory> ZipCode; //New

public ZipCodeValidatorCompiled()
{
    _dbContext = new DataContext(ConfigurationManager.AppSettings.Get("db"));
    ZipCode = _dbContext.GetTable<ZipCodeTerritory>(); //New

    SearchEffectiveDate = CompiledQuery.Compile((DataContext db, ZipCodeTerritory zipCode)
    =>
    (from z in ZipCode //New
    where z.DrmTerrDesc.Equals(zipCode.DrmTerrDesc) &&
            z.IndDistrnId.Equals(zipCode.IndDistrnId) &&
            z.StateCode.Equals(zipCode.StateCode) &&
            (z.ZipCode.Equals(null) ? z.ZipCode.Equals(null) : z.ZipCode.Equals(zipCode.ZipCode))
    select z).ToList()
    );
}

0 个答案:

没有答案