实体框架可以生成DAL代码吗?
我知道实体框架有数据库优先的方法。现在的问题是它是否可以为我生成 DAL(数据访问层)代码(而不是模型)。
I know that entity framework has a database first approach. Now the question is whether it can generate the DAL (data access layer) code (not the models) for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用对象关系映射器 (ORM) 时,通常没有传统意义上的 CRUD 代码。相反,它将这些操作抽象为更加面向对象的操作。
例如,您不“插入”,而是将模型类添加到表中,然后保存更改。 ORM 自动生成使对象模型与数据模型匹配所需的 SQL。
所以我的观点是,你的问题表明你对 ORM 的工作原理以及它们与数据模型的关系缺乏基本的了解。你或许应该读一点书。
When using a Object Relational Mapper (ORM), you don't typically have CRUD code in the traditional sense. Rather, it abstracts those operations into more object oriented operations.
For example, you don't "insert", you add the model class to the table, then save changes. The ORM automatically generates the SQL needed to make the Object model match the data model.
So my point is, your question displays a basic lack of understanding of how ORM's work and how they relate to data models. You should probably do a little reading.
我不确定“DAL 代码”的具体含义,因为这是一个相当模糊的术语。我会认为您的实体类型是 DAL 的一部分。
当您使用模型优先或数据库优先方法时,实体框架工具可以从您的模型自动生成上下文类.edmx,它将继承自
ObjectContext
。通过在线查找一个 T4 模板,可以轻松自定义生成的上下文类已经从 .edmx 生成,并根据您的喜好进行修改。代码优先开发使用
DbContext
,它通常不是自动生成的。请参阅此请在 Scott Gu 的博客上发布,了解更多详细信息。I'm not sure what you mean specifically by "DAL code", as that's a rather ambiguous term. I would consider your Entity types part of the DAL.
When you use a model-first or database-first approach, the Entity Framework tools can auto-generate a context class from your model .edmx, which will inherit from
ObjectContext
. It's easy to customize the generated context class with T4 templates by finding one online that already generates from a .edmx, and modifying to your liking.Code-first development uses the
DbContext
, which is not typically auto-generated. Please see this post on Scott Gu's blog for more details on this.