如何创建实体框架三层解决方案

编程入门 行业动态 更新时间:2024-10-13 10:24:32
本文介绍了如何创建实体框架三层解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个解决方案三个项目,一是网络applciation,为DAL和BLL二级库。创造了DAL层的实体框架模型,并在BLL项目中引用的DAL库。

I created three projects in a solution, one web applciation, two class libraries for DAL and BLL. Created the Entity Framework model in the DAL layer and referenced the DAL library in the BLL project.

当我打电话从Web应用程序项目BLL对象我遇到了问题,它说我需要引用实体框架。我不想在Web应用程序项目在DAL库对象的任何相关性。

When I call the BLL objects from the web application project am running into problems, it says I need to reference entity framework. I dont want any dependency on DAL library objects in the web application project.

有建设使用Entity Framework的清洁三层应用程序了任何具体的指导。

Is there any specific guidance on building clean three-tier applicaiton using Entity Framework.

推荐答案

听起来像你BLL被揭实体您在DAL添加类。你需要创建在BLL包装类(这是POCO),并从DAL返回这些,而不是实体。

Sounds like your BLL is exposing the entity classes you added in the DAL. You'll need to create wrapper classes (That are POCO) in the BLL and return those instead of the entities from the DAL.

这可能是你的是这样做的:

This is probably what you are doing:

// DAL // .edmx file generated entities public IQueryable<TableEntity> GetTableEntities() { // read from entity framework and return } // BLL public IEnumerable<TableEntity> ReadTableEntitiesForUser(int userID); { var d = new DAL(); var entities = d.GetTableEntities(); // restrict to entites this user "owns" entities = entities.Where(e => e.OwnerID.Equals(userID)); return entities; } // WebApp var b = new BLL(); var myEntities = b.ReadTableEntitiesForUser(1234);

这可能是你的应做:

// DAL // .edmx file generated entities public IQueryable<TableEntity> GetTableEntities() { // read from entity framework and return } // BLL public class TableEntityDTO { public int ID { get; set; } public string Name { get; set; } // continue on for each column in the table // and make a DTO class for each table in your database } public IEnumerable<TableEntityDTO> ReadTableEntitiesForUser(int userID); { var d = new DAL(); var entities = d.GetTableEntities(); // restrict to entites this user "owns" entities = entities.Where(e => e.OwnerID.Equals(userID)); // convert from "Entity Framework Object" to "BLL Object" foreach(var e in entities) { yeild return new TableEntityDTO() { ID = e.ID, Name = e.Name }; } } // WebApp var b = new BLL(); var myEntities = b.ReadTableEntitiesForUser(1234);

这是对于使用.NET 3.5SP1和LINQ到SQL这两者我都用过了一下,可能为EF的最新versons真正附带的实体框架正确的,但有code -first和其他东西有可能避免这种额外数据传输-对象工序的方式,尽管使用面向服务的体系结构,DTO的可能最好的方式。

This is true for the Entity Framework that shipped with .NET 3.5SP1 and for Linq-To-SQL both of which I have used a bit, it may hold true for the latest versons of EF, but with Code-First and other things there may be a way to avoid this extra Data-Transfer-Object step, though with a Service Orientated Architecture, DTOs are likely the best way to go.

更多推荐

如何创建实体框架三层解决方案

本文发布于:2023-11-29 11:30:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   解决方案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!