实体框架代码优先模型与领域的分离 [英] Entity Framework Code First model separation from domain

查看:103
本文介绍了实体框架代码优先模型与领域的分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架代码第一个最佳实践问题?

Entity Framework Code First best practice question?

大家好,我在NTier应用程序上使用EF codeFirst 6.

Hi All I am using EF codeFirst 6 on an NTier app.

我发现我用来映射到EF的poco对象确实是特定于EntityFramework的.让我给你一个例子

I have found that poco object that I am using to map to EF are really EntityFramework specific. Let me give you an example

如果我想在对象中添加与EF不相关的属性,则EF不喜欢它. 我阅读了您可以放置​​"NotMapped"属性的信息,但是它开始使该对象难以维护.

If I want to add a property that is not related to EF in the object ,EF does not like it. I Read you can put the "NotMapped" attribute however it start making this object difficult to maintain .

也可能有一些开发人员不熟悉EF,并且不了解该问题.

Also there might be developers that are not familiar with EF and that will not understand the issue.

我的问题是,保持EF实体模型分离并有dto转换成域模型或从中转换成域模型的良好实践是什么? 开发人员可以在不干扰EF Model的情况下完成自己喜欢的事情,而EF Model与数据库中的表显然是一对一的

My question is it good practice to keep EF Entity Models separate and have a dto to convert to/from to a Domain Model where a developer can do what he likes with it without interferring with EF Model which is clearly a 1 to 1 with the tables in the database

有什么建议吗?

推荐答案

可以使用Fluent API而不是Attribute-based(实体框架Fluent API .

Your problem could be resolved by using the Fluent API approach instead of the Attribute-based (Annotations) approach. See Entity Framework Fluent API.

您将在DBContext中而不是在实体类中配置实体映射.

You would configure your entity mappings in the DBContext rather than in the entity classes.

来自上面的链接文章:

指定不将CLR属性映射到数据库中的列

Specifying Not to Map a CLR Property to a Column in the Database

以下示例显示如何在CLR上指定该属性 类型未映射到数据库中的列.

The following example shows how to specify that a property on a CLR type is not mapped to a column in the database.

modelBuilder.Entity<Department>().Ignore(t => t.Budget);

表示忽略Department实体中的Bugdet属性".

that would mean "ignore the Bugdet property in the Department entity."

这篇关于实体框架代码优先模型与领域的分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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