Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net &引用;附「;不';似乎无法用于导航属性集合_Vb.net_Entity Framework_Visual Studio 2015_Entity Framework 6_Ado.net Entity Data Model - Fatal编程技术网

Vb.net &引用;附「;不';似乎无法用于导航属性集合

Vb.net &引用;附「;不';似乎无法用于导航属性集合,vb.net,entity-framework,visual-studio-2015,entity-framework-6,ado.net-entity-data-model,Vb.net,Entity Framework,Visual Studio 2015,Entity Framework 6,Ado.net Entity Data Model,查看该问题的前两个答案(请参阅文章)表明,导航属性集合(即entity1.Entity2Collection.Attach(entity2))存在一个Attach方法。问题是,我有VS 2015/EF 6.1.3(VB和DBContext),而导航属性集合似乎不存在该方法(或者至少Intellisense没有显示)。我做错了什么吗?Dannie f.的解决方案(为VB重新编码)如下所示,假设一个TopicDBEntities模型,以及Topic和Subscription的实体集合: 您是对的,没

查看该问题的前两个答案(请参阅文章)表明,导航属性集合(即entity1.Entity2Collection.Attach(entity2))存在一个Attach方法。问题是,我有VS 2015/EF 6.1.3(VB和DBContext),而导航属性集合似乎不存在该方法(或者至少Intellisense没有显示)。我做错了什么吗?

Dannie f.的解决方案(为VB重新编码)如下所示,假设一个TopicDBEntities模型,以及Topic和Subscription的实体集合:


您是对的,没有可用于导航属性集合的此类方法。你可能会看这篇文章,不确定它是否适用于你的案例。附件不是实体6.1.3的一部分,它在以前的一些版本中已被删除。我发现页面上的第一个答案与我链接的问题不是EF 6.1.3的正确答案;第二个答案(dannie.f的答案)是正确的。您必须创建第一个实体的分离实例,将第二个实体添加到其nav prop集合中,附加(未附加的)第一个实体,然后执行删除操作。如果希望在不首先加载的情况下添加两个实体之间的关系,我想dannie.f的代码仍然可以使用——只需将Remove语句替换为Add,对吧?我想我知道事情会成功的。EF仅将对实体属性的更改写入存储。因此,在调用SaveChanges或DetectChanges时,实体属性(包括集合导航属性)的“绝对”状态无需正确,只要自上次附加/保存更改/检测更改/查询以来所做的更改(添加、删除、修改)正确即可。顺便问一下,创建连接实体的分离克隆的最佳方法是什么?
Dim db = New TopicDBEntities()

'   Create UNATTACHED instances of the two entities and associate them
'   (In real life, you would have something more sophisticated for the
'   next 2 lines)
Dim topic = New Topic { .TopicId = 1 }
Dim subscription = New Subscription { .SubscriptionId = 2}

topic.Subscriptions.Add(subscription)

'   Attach the topic and subscription as unchanged 
'   so that they will not be added to the db   
'   but start tracking changes to the entities
db.Topics.Attach(topic)

'   Remove the subscription
'   EF will know that the subscription should be removed from the topic
topic.subscriptions.Remove(subscription)

'   commit the changes
db.SaveChanges()