【发布时间】:2013-09-12 20:55:11
【问题描述】:
我有一个包含我所有实体框架代码的类库。
我正在开发一个涉及多个子项目的解决方案,包括一个 ASP.NET MVC 项目。我的模型已被分离到一个单独的程序集中,因为我需要从解决方案中的各种其他项目中使用它。
我将我的解决方案分为三层:
1) `DAL` (Data Access Layer Entity Framework .EDMX) DATABASE First approach...
2) `Service` layer (will be calling DAL to get data from db....)
3) `UI` (which will be MVC 4 framework)
所以在我的服务项目中,我引用了数据访问 dll
在 ASP.NET MVC 项目中我有服务项目的参考
但是,我现在开始构建一个 ASP.NET MVC。
我尝试添加控制器并从我的 DAL 中选择模型类收到错误
Error 1 The type 'myproj_dal.requester' is defined in an assembly that is not referenced. You must add a reference to assembly 'myproj_dal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. \issoa_ui\Controllers\RequesterController.cs
这是我的控制器的样子:
public ActionResult Index()
{
issoa_service.RequesterService reqService = new issoa_service.RequesterService();
var model = reqService.GetRequesters(); //**<<<ERROR**
return View(model);
}
在我的Web.Config 中,我的连接字符串与我在DAL app.config. 中的完全一样
这是我的RequesterService,看起来像这样:
public class RequesterService
{
db_entities edmx = new db_entities();
public IList<Requester> GetRequesters()
{
IList<Requester> model = edmx.Requester.ToList();
return model;
}
}
Model.edmx
>>>>>Model.tt
>>>Requester.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace myproj_dal
{
using System;
using System.Collections.Generic;
public partial class Requester
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
}
}
我的问题是:
1) 为什么会出现上述错误,是否需要将 DAL 的引用添加到 UI?如果是,那么我就违背了松散耦合的全部目的。
2) 正确的做法是什么;如果我没有按照最好的标准做。
任何人都可以指点我或纠正我或对我大喊大叫或将我重定向到某种解释或示例项目会有所帮助吗?
【问题讨论】:
-
为此,您需要在 Web 项目中添加对 DAL 的引用。当然,如果您想在另一个项目中使用来自一个项目的类,则必须这样做。如果您想避免耦合它们,为什么不为您的实体模型创建一个单独的项目,您可以从两个需要它的项目中引用它?
-
您是说将 DAL 引用添加到我的 MVC 项目中?这是我必须解决的唯一选择?
-
如果你想在另一个项目中使用一个项目中的一个类,你必须引用它。时期。这就是参考的全部意义。
-
我确实为实体模型创建了单独的项目,请阅读我的问题
-
是的,我了解参考,DAL 参考在我的服务中,那么为什么我应该在 Web 项目中使用 DAL 参考?那部分我不明白
标签: entity-framework asp.net-mvc-4 entity-framework-5 ado.net-entity-data-model