[英]C# Entity framework, how to get the primary key from an object created from a Junction table
Create new APN Object and add IPSec object via the IPSecs Junction table. 创建新的APN对象,并通过IPSec交界表添加IPSec对象。 I then need to add the new IPSecID (primary Key) to the BuildType table.
然后,我需要将新的IPSecID(主键)添加到BuildType表中。
APN newApn = new APN()
newApn.IPSecs.Add(new IPSec
{
EndPoint = vtcPriEndPoint.Text,
}
newApn.BuildTypes.Add(new BuildType
{
TypeOfBuild = "IPsec",
TransportID = newApn.IPSecID <---no option for this syntax
}
As you have defined a many to many relation between APN
and IPSec
you will have to for each
through the newApn's IPSecs and create BuildTypes
for each of them. 正如你所定义的许多人之间有很多关系
APN
和IPSec
,你将不得不for each
通过newApn的IPSecs创造BuildTypes
为他们每个人。
That said, the Id
of the new IPSecs
will not be known until after you have called SaveChanges
on the context. 就是说,直到您在上下文中调用
SaveChanges
之后,才能知道新IPSecs
的Id
。 Alternatively you can assign each IPSec
of the newAPN to the BuildType
's supposed Transport
Navigation property. 或者,您可以将newAPN的每个
IPSec
分配给BuildType
的假定Transport
导航属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.