简体   繁体   English

实体框架DBFirst将表转换为关联,如何获取表访问权限?

[英]Entity Framework DBFirst turns table into association, how to get table access?

I have a DBFirst EntityFramework 6.1 solution that i'm trying to generate off of. 我有一个试图生成的DBFirst EntityFramework 6.1解决方案。 When i add a table that only contains two foreign keys the table is turned into two associations and I can not directly access the table anymore. 当我添加仅包含两个外键的表时,该表将变成两个关联,并且我无法再直接访问该表。 This is neat for navigation in the code but makes it a pain in the ass to delete records from the table. 这很容易在代码中导航,但是从表中删除记录很麻烦。

Is there a way to prevent this behavior and gain direct access to the table as an entity? 有没有办法防止这种行为并以实体的形式直接访问表?

For example i am unable to remove an entry in the association because i get this error 例如,我无法删除关联中的条目,因为出现此错误

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 When a change is made to a relationship, the related foreign-key property is set to a null value. 对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

For example here is how my database sees the structure. 例如,这是我的数据库如何看待结构。

数据库图

Here is how it appears in entity framework. 这是它在实体框架中的显示方式。 Notice that the CorporateDataShareVisible table is missing and instead two new associations are created. 请注意,缺少CorporateDataShareVisible表,而是创建了两个新的关联。

EntityFrameworkDiagram

The CorporateDataShareVisible table should be able to be deleted and added to at will but any changes i make seem to stop it from working. CorporateDataShareVisible表应该可以删除并随意添加,但是我所做的任何更改似乎都使其无法正常工作。

Add a primary key to your table that has only foreign keys. 将一个主键添加到只有外键的表中。 EF uses the primary key to keep track internally of the element. EF使用主键来跟踪元素的内部。 Without a primary key it doesnt know which element was modified and how to send that back to your RDBMS. 没有主键,它不知道修改了哪个元素以及如何将其发送回RDBMS。

I prefer surrogate keys ie auto incrementing integers. 我更喜欢代理键,即自动递增整数。 You can also add the primary key by making it a composite key of both the foreign keys 您还可以通过将主键设为两个外键的组合键来添加主键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM