实体框架代码优先模型与领域的分离

编程入门 行业动态 更新时间:2024-10-24 20:15:37
本文介绍了实体框架代码优先模型与领域的分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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."

更多推荐

实体框架代码优先模型与领域的分离

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

发布评论

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

>www.elefans.com

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