Compile ERROR: The type is defined in an assembly that is not referenced

Compile ERROR: The type is defined in an assembly that is not referenced

我正在开发 3 层应用程序。我使用 O/R 设计器向 SQL table (LEAVE) 添加了一个 Linq。我添加了下面的方法,用于从业务层的 class (LeaveRecord) 中的 table (LEAVE) 检索数据。

public IQueryable<LEAVE> getLeaves()
{
    dbContext db = new dbContext;
    return db.LEAVEs;
}

在 UI 层上,在网络表单的隐藏代码中,我尝试这样获取数据:

bll.LeaveRecord leaveRecords = new bll.LeaveRecord();
var data = leaveRecords.getLeaves(); // the error message highlights this line (41) as the offender

当我运行程序时,出现编译错误:

The type 'programname.dal.LEAVE' is defined in an assembly that is not referenced. You must add a reference to assembly 'programname.dal, version=1.0.0.0, Culture=neutral, PublicKeyToken=null' (Line 41).

我该如何解决这个问题?

您正在公开 bll 中的 dal 数据类型。因此,如果有人想使用 bll API(即您的 UI),则还需要引用 dal

因此您可以选择从 UI 引用 dal 或在 bll 中进行数据类型切换并公开 [=] 中定义的新数据类型11=].

最简单的解决方案是从 UI 项目添加对 dal 的引用。

您必须整理数据对象:

public DaoLeave
{
    // Properties of DaoLeave, same as properties of LEAVE

    internal void DaoLeave(LEAVE doLeave)
    {
        // set properties from properties ... alternately, use something like AutoMapper
    }
}

public IQueryable<DaoLeave> getLeaves()
{
    dbContext db = new dbContext;
    return db.LEAVEs.Select(l => new DaoLeave(l));
}