Entity Framework 4.1 Code First - 如何将包含实体 ID(外键)的 csv 的字符串映射到相应的实体
在提交问题之前,请注意,我正在使用第三方供应商拥有的现有数据库,因此不幸的是,无法更改数据库格式。
问题是:我有一个映射到数据库表的实体,该表有一个 varchar 列,其中包含 csv 格式的一对多外键。这些外键对应于另一个实体类型的 ID。我一直在编写一个函数,从该 csv 列表创建 ID 列表,然后通过 DBContect 对象搜索该实体。我想做的是映射实体之间的关系。有办法做到这一点吗?谢谢!
Before I submit my question, please be aware that I'm working with an existing database owned by a third party vendor, so unfortunately changing the database format is not an option.
Here's the issue: I have an Entity mapped to a database table that has a varchar column that contains one to many foreign keys in csv format. Those foreign keys correspond to the ID's of another Entity type. What I've been doing is writing a function that creates a List of ID's from that csv list and then I search for that Entity through the DBContect object. What I'd like to do is map a relationship between the entities. Is there a way to do that? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,如果不更改数据库,就无法做到这一点。 EF是ORM工具,但它仍然非常依赖于数据库设计的正确性。在单列中存储多个值甚至破坏了第一个数据库范式。对于 EF,包含 csv 数据的列是单个字符串值,您无法与该值建立关系。
顺便提一句。它甚至更复杂,因为该列无法表示标准关系含义中的一对多关系 - 这需要依赖实体包含主实体的 Id,而不是主实体包含所有依赖实体的 Id。
Unfortunately there is no way to do that without changes in the database. EF is ORM tool but it is still very dependent on correctness of database design. Storing multiple values in single column is breaking even first database normal form. For EF your column containing csv data is single string value and you cannot make relation on that value.
Btw. it is even more complicated because the column cannot represent one-to-many relation in standard relational meaning - that would require dependent entities to contain Id of your master entity, not that master entity contains Ids of all dependent entities.