在实体框架中定义业务对象
尝试了解实体框架。我的方法是数据库优先。但是,我想在模型中定义更接近我的业务对象的其他实体。我想我可以在数据库中编写查询并将它们包含在模型中。但我还想在模型中定义全新的实体,尽管它们将基于数据库中的基础表。我该怎么做 - 有人知道教程吗?
问候 比约恩 db 老朋友,EF 新手
Trying to understand Entity Framework. My approach is database first. However I would like to define other entites in the model that is closer to my business objects. I guess I could write queries in the db and include them in the model. But I would also like to define entirely new entities in the model though they would be based on underlying tables in the db. How do I do that - does anyone know a tutorial?
Regards
Bjørn
db Oldtimer, EF Newbie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库优先意味着您拥有现有数据库,您可以通过从数据库更新或手动创建模型。您可以使用向导创建初始模型并手动修改它以定义新实体,但您不能再使用数据库更新,否则某些更改将被删除。此外,您的自定义修改必须遵循 EF 映射规则(例如,不可能直接将多个实体映射到同一个表,除了一些更高级的映射场景,如拆分和继承),其中一些(自定义查询)必须直接在 EDMX 中完成源(XML),因为设计器不支持它们 - 这需要更复杂的 EF 映射知识,对于新手来说肯定很难。
您可以检查该 XML 的规范。对于映射到自定义查询的实体,您必须使用 SSDL 中的 DefiningQuery 元素EDMX 的一部分。
Database first means that you have existing database and you can either create model by updating from database or manually. You can use wizard to create initial model and modify it manually to define new entities but you must not use update from database any more or some of your changes will be deleted. Also your custom modifications must follow EF mapping rules (for example it is not directly possible to map multiple entities to the same table except some more advanced mapping scenarios like splitting and inheritance) and some of them (custom queries) must be done directly in EDMX source (XML) because designer doesn't support them - this requires more complex knowledge of EF mapping and it will be definitely hard for newbie.
You can check specification of that XML. For entities mapped to custom queries you will have to use DefiningQuery element in SSDL part of EDMX.