实体框架4
我决定将 DAL 转换为实体框架 4.0。
我已经拥有庞大的数据库和许多业务课程。
如何将表和字段映射到我的类?
I decided to convert my DAL to Entity Framework 4.0.
I already have my huge database and also many business classes.
How can I map the tables and fields to my classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以给这只猫剥皮。 AutoMapper 是我想到的一个。
您还可以使用 LINQ 手动执行此操作,例如:
您还可以编写自己的方法,使用反射按类型和名称映射属性。
...只是抛出一堆想法。
There are many ways to skin this cat. AutoMapper is one that comes to mind.
You can also do it manually with LINQ, for example:
You can also write your own method that uses Reflection to map properties by type and name.
...Just throwing a bunch of ideas out there.
实体框架允许您为“概念”域(您的对象)创建映射,并为“存储”域(您的数据库)创建单独的映射。然后,您可以使用中心概念存储映射规范将它们映射在一起。听起来很复杂,不幸的是......EF 不是一个轻量级的工具。
有很多东西需要学习,但首先要阅读 CSDL 、SSDL 和实体框架的 MSL 规范。
如果您想使用当前的对象和数据库,您可能必须在 CSDL、SSDL 和 MSL 中定义自定义映射。如果您想让您的生活更轻松,您可以使用内置的 Visual Studio 工具生成默认的实体数据模型。从现有数据库创建 EDM 将生成映射到数据库的对象,但有大约一百万种方法可以自定义此过程,并且有多种方法可以定义对象以便它们可以与 EF 一起使用。
我建议从数据库创建默认 EDM,并查看生成的 .edmx 文件。 .edmx 通常包含 XML 格式的 CSDL、SSDL 和 MSL,以及生成的代码。 (同样,这可以完全定制)。您可能还想花一些时间阅读 EF...这是一头野兽。
Entity Framework allows you to create a mapping for your "conceptual" domain (your objects), and a separate mapping for the "storage" domain (your database). You then map these together using a central conceptual<->storage mapping specification. It sounds complicated, and unfortunately it is... EF is not a lightweight tool.
There's a lot to learn, but a place to start would be to read up on the CSDL, SSDL, and MSL specifications for Entity Framework.
If you want to use your current objects and database, you may have to define custom mappings in CSDL, SSDL, and MSL. If you want to make your life easier, you could generate a default Entity Data Model using the built-in Visual Studio tools. Creating an EDM from an existing database will generate objects that map to your database, but there are about a million ways to customize this process, and there are several ways to define your objects so that they can be used with EF.
I would suggest creating a default EDM from your database, and take a look at the .edmx file that is produced. An .edmx usually contains the CSDL, SSDL, and MSL in an XML format, along with generated code. (Again this can be totally customized). You might want to also spend some time reading up on EF... it's a beast.