English 中文(简体)
如何为航行财产找到外国关键领域?
原标题:How to find foreign key field for navigation property?

For a given entity I can get all its references by calling

var references = dbContext.Entry(entity).References;

现在,我想把所有提法都提到无效。

foreach (var reference in references)
{
  reference.CurrentValue = null;
}
context.Update(entity);
context.SaveChanges();

但是,似乎仅仅提及无效是不够的,还必须确定外国关键财产。 但是,如何找到外国关键领域来加以参考?

最佳回答

You can use Metadata property to get the associated INavigation, then ForeignKey property to get the associated IForeignKey, then the Properties property to get the associated properties of type IProperty, and finally the Name property which can be passed to Property method to obtain the associated PropertyEntry for manipulating the value.

采取行动:


var entry = dbContext.Entry(entity);
foreach (var reference in entry.References)
{
    reference.CurrentValue = null;
    foreach (var fkProperty in reference.Metadata.ForeignKey.Properties)
        entry.Property(fkProperty.Name).CurrentValue = null;
}

<>Update: 在最近的核心版本中,<代码>Metadata 房地产类型已改为INavigation 基地(可能作为准备航道性能支助的一部分)没有<编码>ForeignKey 财产,因此,需要人工将其投到上述<代码>INavigation接口,例如:

using Microsoft.EntityFrameworkCore.Metadata;
...
foreach (var fkProperty in ((INavigation)reference.Metadata).ForeignKey.Properties)
...
问题回答

暂无回答




相关问题
nhibernate hasmany when not using foreign key

Let say I have a request table, that looks as follows: RequestId INT ReferenceNumber VARCHAR Each request is logged in a requestlog table. There are no foreign keys between the tables: ...

Handling foreign key exceptions in PHP

What is the best way in PHP to handle foreign key exceptions on a mysql database? Is there a mysql class that can be used to simplify any code? Ideally, what I want to do, as an example, is to try to ...

Oracle cyclic foreign key references issues

I ve been racking my brain trying to come up with a solution to this. For a database class, I need to implement the following: Table HUSBANDS: (Name Varchar2(10)) (Wife Varchar2(10)) Table WIVES: (...

Issues when adding foreign key

When I m trying to create a foreign key linked to a primary key in another table I get: #1452 - Cannot add or update a child row: a foreign key constraint fails (`fayer`.`#sql-225c_1d3`, CONSTRAINT `#...

Can someone explain MySQL foreign keys

I know what they are my question is, how do you link them or are they automatically linked when you have identical names in different tables. Here is an example: Say I have an [orders] table and a [...

How to emulate tagged union in a database?

What is the best way to emulate Tagged union in databases? I m talking about something like this: create table t1 { vehicle_id INTEGER NOT NULL REFERENCES car(id) OR motor(id) -- not valid ... } ...

热门标签