如何使用不包含外键的现有数据库创建实体之间的关系 [英] How to create relationships between entities with existing database that does not contain foreign keys

查看:68
本文介绍了如何使用不包含外键的现有数据库创建实体之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,EF团队不再在论坛上正式活跃。我最近访问了Julie Lerman的博客,该博客仍然将此资源作为发布有关EF事宜的好位置。我已经在StackOverflow上发布了一个副本,但是
还没有回复。


如果论坛中有人可能有替代方法来完成我想要实现的结果我真的很感激任何输入。此外,如果有经验的人同意我的解决方案也会有所帮助。 


对于任何跨网站重复发布提前抱歉




使用Entity Framework Core 2.0



坚持使用公司的生产数据库为每个表定义的键,但没有为任何关系定义外键。



数据库中的相关记录具有打算的id字段ed与父记录的主键字段相关,就像通常使用外键关系/约束一样。但是这些字段都被创建为INT NOT NULL并使用
a SQL默认值为'0'。



因此插入了相关记录随着时间的推移,不要求指定相关的父记录。



最初我用EF在整数中定义了我的模型,并使用了一个流畅的配置来指定"IsRequired"。 。这样做了,所以我可以运行迁移来创建一个测试数据库,用于与生产数据库进行比较,以验证我的代码首先是
是否正确编码。



这会在使用"包含"时导致问题。在我的Linq查询中执行内部联接,导致在依赖记录的id字段中删除包含0的记录。



唯一的方法是我发现这项工作是将依赖实体中的所有id字段建模为可空整数,并删除"IsRequired"。从流畅的配置。



使用"Include"时它执行左外连接,保留所有依赖实体。这也意味着包含的实体上的任何引用属性都设置为null而不是空字符串。这部分可能很容易修复



缺点是我现在想要使用迁移创建数据库,所有id字段都在依赖记录将创建为NULL。



有没有人遇到过这种情况?有没有人有任何建议
尝试我正在使用的方法以外?


解决方案

嗯,有些人需要在你工作的公司用手枪鞭打造成这种关系型数据库灾难。 


你不能做出一些东西,只有粪甲虫可以做到这一点。  


恕我直言,您应该放弃使用任何ORM。您应该考虑使用ADO.NET,SQL Command对象,参数化内联T-SQL或  参数化的sprocs, 
一个数据加载器和CRUD的DTO模式,在列表< T>中使用单个DTO或DTO。 


https:// www .codeproject.com /文章/ 1050468 /数据传输-对象的设计图案中-Csharp的


I understand that the EF team is no longer officially active on the forum. I recently visited Julie Lerman's Blog which still reference this resource as a good location to post concerning EF matters. I have posted a duplicate on StackOverflow as well but no responses as of yet.

If someone in the forum may have an alternative method to accomplish the result I am trying to achieve I would really appreciate any input. Also if someone of experience agrees with my solution that would be helpful to know as well. 

Sorry in advance for any across site duplication posting

Using Entity Framework Core 2.0

Stuck with company's production database which has primary keys defined for each table but no foreign keys defined for any relationships.

Dependent records in the database have id fields which are intended to relate to the primary key fields of the parent record like you would normally find with a foreign key relationship/constraint. But these fields were all created as INT NOT NULL and are using a SQL default of '0'.

As a result dependent records have been inserted over time without requiring that a related parent record be specified.

Initially I defined my models in EF with integers and used a fluent configuration to specify "IsRequired". This was done so I could run migrations to create a test database for comparison against the production database to verify that my code first was correctly coded.

This then lead to the problem while using "Include" in my Linq queries which performs an inner join that results in dropping the records that contain the 0's in the id fields of the dependent record.

The only way that I have found to make this work is to model all of the id fields in the dependent entity as nullable integers and remove the "IsRequired" from the fluent configuration.

When using the "Include" it performs a left outer join keeping all of the dependent entities. This also means that any reference properties on the included entities are set to null instead of an empty string. This part can probably be fixed fairly easily.

The downside is if I wanted to use migrations to create a database now, all id fields in the dependent records would be created as NULL.

Is there anyone who has run up against this type of situation? Does anyone have any suggestions to try other than the approach I am using?

解决方案

Well, some people need to be pistol-whipped at the company you are working at for creating such a relational database disaster. 

You can't make something out of s**t, only a dung beetle can do that.  

IMHO, you should abandon using any ORM. You should look into using ADO.NET, SQL Command objects, parametrized inline T-SQL or parametrized sprocs,  a datareader and the DTO pattern for CRUD with a single DTO or DTO(s) in a List<T>. 

https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp


这篇关于如何使用不包含外键的现有数据库创建实体之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆