ORM是否可能与实体框架? [英] Is ORM possible with Entity Framework?

查看:115
本文介绍了ORM是否可能与实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用EF在我们的域实体和我们的表之间使用ORM实现数据层。我们已经成功地完成了与其各自表格非常相似的领域实体的简单映射,但是在尝试将对象与更复杂的关系映射到对象时,这些关系包含外键在表表示中。我已经发现很多关于表拆分的参考,使用EF与共享主键的表,但是我们的模式不是这样。

We’re trying to implement a data layer with ORM between our domain entities and our tables using EF. We’ve successfully done trivial mappings with domain entities that closely resemble their respective tables, but have stumbled when trying to map objects with more complex relationships that include foreign keys in their table representations. I’ve found plenty of references to "Table Splitting" using EF with tables that share primary keys, but that’s not the case with our schema.

作为一个例子,我喜欢将域实体作为POCO,而不是比简单的ID更了解数据层:

As an example, I’d like to have domain entities as POCO’s with no more awareness of the data layer than simple ID’s:

public class EntityInfo
{
    public int EntityId { get; set; }
    public string EntityName { get; set; }
    public string TypeName { get; set; }
    public string ComponentName { get; set; }
}

public class Entity
{
    public int EntityId { get; set; }
    public EntityType Type { get; set; }
    public EntitySource Source { get; set; }
    public string Name { get; set; }
}

public class EntityType
{
    public int TypeId { get; set; }
    public string Name { get; set; }
}

public class EntitySource
{
    public int SourceId { get; set; }
    public string Name { get; set; }
}

这些将映射到一系列具有外键的等效表: / p>

These would be mapped to a series of equivalent tables with foreign keys:

Table Entities:
[PK]    int     EntityId
[FK]    int     TypeId
[FK]    int     SourceId
        string  Name

Table EntityTypes:
[PK]    int     TypeId
        string  Name

Table EntitySources:
[PK]    int     SourceId
        string  Name

我们真正想做的是分开完全从域实体的表表示,并实现映射层(.msl)来解决这些有些复杂的关系。上面的EntityInfo类将会建立并存储到表中,尽管没有自己的直接表表示。这样的映射(多个表到对象层次结构)甚至可能使用.msl?是否可以用EF?

What we’d really like to do is separate the table representation from the domain entities entirely and implement the mapping layer (.msl) to resolve these somewhat complex relationships. The EntityInfo class above would be built from and store to the tables, despite having no direct table representation of its own. Is such a mapping (multiple tables-to-object hierarchy) even possible using an .msl? Is it possible with EF?

推荐答案

你至少尝试过吗?您所提供的模型中没有复杂的映射。这是一对多关系的最基本的映射。只需在设计器中打开EDMX,并从数据库命令(从上下文菜单)运行更新。在向导中选择您的三个表,并取消选中将外键添加到模型中。

Did you at least try it? There is no complex mapping in your presented model. That is the most basic mapping of one-to-many relationships. Simply open EDMX in the designer and run Update from database command (from context menu). Select your three tables in the wizard and uncheck option for adding foreign keys to the model.

编辑:

是的,实体框架能够映射甚至你的 EntityInfo 对象,但是对象将被读取,因为所有关于它包含的相关实体(它们的FK关系)的信息将被丢失。有两种方法可以映射:

Yes entity framework is able to map even your EntityInfo object but the object will be read only because all information about what related entities (their FK relations) it includes will be lost. There are two ways to map it:


  • 使用 DefiningQuery DefiningQuery 是用于加载要映射的列的自定义SQL。 EF不会打扰您加入多少表以获取结果集。问题是,默认的设计人员将在每次尝试从数据库更新模型时删除 DefiningQuery

  • 在MSL中使用 QueryView QueryView 是映射到新内容的自定义实体SQL视图。它建立在您的模型中定义的实体上。所以你必须映射 Entity EntityType EntitySource (但您可以将它们作为数据组件的内部)。 QueryView 存储数据库中的更新。

  • Use DefiningQuery in SSDL. DefiningQuery is custom SQL used to load columns you want to map. EF doesn't bother how many tables you join to get your result set. The problem is that default designer will delete your DefiningQuery each time you try to Update model from database.
  • Use QueryView in MSL. QueryView is custom Entity SQL view mapped to a new enttiy. It is built on entities defined in your model. So you must map Entity, EntityType and EntitySource as well (but you can make them for example internal to your data assembly). QueryView survives updates from database.

DefiningQuery必须直接在EDMX(以XML打开)中定义QueryView ,因为设计者不支持和。这些功能在代码优先方法中不可用。这些功能创建只读实体,所以如果您想要CUD操作,您也必须创建存储过程并将其映射到新创建的实体。

Both DefiningQuery and QueryView must be defined directly in EDMX (opened as XML) because designer doesn't support them. These features are not available in code-first approach. These features create read only entities so if you want CUD operations as well you have to create stored procedures and map them to newly created entities.

还有最常用的最后一个选项 - 您可以创建linq-to-entity查询,简单地将映射的实体投射到未映射的 EntityInfo

There is also one last option most commonly used - you can create linq-to-entities query simply projecting your mapped entities to unmapped EntityInfo:

var infos = context.Entities
                   .Select(e => new EntityInfo
                        {
                            EntityId = e.EntityId,
                            EntityName = e.Name,
                            TypeName = e.Type.Name,
                            ComponentName = e.Source.Name 
                        });              

这篇关于ORM是否可能与实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆