简体   繁体   English

C#实体框架,如何从通过Junction表创建的对象中获取主键

[英]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. 正如你所定义的许多人之间有很多关系APNIPSec ,你将不得不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之后,才能知道新IPSecsId 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.

相关问题 代码优先实体框架C#,多对多映射,创建连接表,但未在对象上填充列表属性 - Code First Entity Framework C#, Many to Many Mapping, Junction Table Created but list property not populated on object 如何获取实体框架连接表的计数 - How to get the Count of an Entity Framework Junction Table 实体框架-如何从联结表中删除数据 - Entity framework - how to remove data from junction table 如何使用linq和C#中的实体框架从另一个表中获取对象中的项目列表? - how to get a list of item in object from another table using linq and entity framework in C#? 如何从Entity Framework更新主键? - How to update primary key from Entity Framework? 如何使用 Entity Framework Core 从数据库表中确定通用实体的下一个主键? - How to determine the next primary key from a database table for a generic entity using Entity Framework Core? 如何使用c#将对象(表实体)添加为字典中表中字段之一作为值和键 - How to add a object (table entity) as the value and the key being one of the field from the table in a dictionary using c# 从实体框架5.0中的模型获取主键名称 - Get primary key name from model in entity framework 5.0 在实体框架中通过键从表获取行 - Get a row from A Table By Key in Entity Framework 如何使用LINQ从表中选择存在于联结表中的元素-Entity Framework - How to select elements from a table that are present in a junction table with LINQ - Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM