Entity framework 如何在实体框架中删除多对多关系而不加载所有数据

Entity framework 如何在实体框架中删除多对多关系而不加载所有数据,entity-framework,ado.net,many-to-many,Entity Framework,Ado.net,Many To Many,有人知道如何在ADO.NET实体框架中删除多对多关系而不必加载所有数据吗?在我的例子中,我有一个实体主题,它有一个属性订阅,我需要删除一个订阅。代码myTopic.Subscriptions.Remove(…)有效,但我需要先加载所有订阅(例如myTopic.Subscriptions.load()),我不想这样做,因为有很多订阅(我指的是很多订阅)订阅数量。一种方法是使用存储过程,直接在数据库中删除您的子记录,并将其包含在EF模型中;然后从您的DataContext调用它。您可以附加()订阅,

有人知道如何在ADO.NET实体框架中删除多对多关系而不必加载所有数据吗?在我的例子中,我有一个实体主题,它有一个属性订阅,我需要删除一个订阅。代码myTopic.Subscriptions.Remove(…)有效,但我需要先加载所有订阅(例如myTopic.Subscriptions.load()),我不想这样做,因为有很多订阅(我指的是很多订阅)订阅数量。

一种方法是使用存储过程,直接在数据库中删除您的子记录,并将其包含在EF模型中;然后从您的DataContext调用它。

您可以附加()订阅,然后删除它-注意,我们这里不使用Add(),只使用Attach,因此我们有效地告诉EF我们知道该对象已附加到存储中,并要求它表现得好像这是真的一样

var db = new TopicDBEntities();
var topic = db.Topics.FirstOrDefault(x => x.TopicId == 1);

// Get the subscription you want to delete
var subscription = db.Subscriptions.FirstOrDefault(x => x.SubscriptionId == 2);
topic.Subscriptions.Attach(subscription); // Attach it (the ObjectContext now 'thinks' it belongs to the topic)
topic.Subscriptions.Remove(subscription); // Remove it
db.SaveChanges(); // Flush changes
整个交换(包括从数据库获取原始主题)将向数据库发送以下3个查询:

SELECT TOP (1) 
[Extent1].[TopicId] AS [TopicId], 
[Extent1].[Description] AS [Description]
FROM [dbo].[Topic] AS [Extent1]
WHERE 1 = [Extent1].[TopicId]


SELECT TOP (1) 
[Extent1].[SubscriptionId] AS [SubscriptionId], 
[Extent1].[Description] AS [Description]
FROM [dbo].[Subscription] AS [Extent1]
WHERE 2 = [Extent1].[SubscriptionId]


exec sp_executesql N'delete [dbo].[TopicSubscriptions]
where (([TopicId] = @0) and ([SubscriptionId] = @1))',N'@0 int,@1 int',@0=1,@1=2

因此,它在任何时候都不会获取所有订阅。

这是我的示例……我知道外键,但我不想做db往返。
我希望这对某人有帮助

给定:
[客户][药物]

Client objClient = new Client() { pkClientID = pkClientID };
EntityKey entityKey = _commonContext.CreateEntityKey("Client", objClient);
objClient.EntityKey = entityKey;
_commonContext.Attach(objClient); //just load entity key ...no db round trip

Medication objMed = new Medication() { pkMedicationID = pkMedicationID };
EntityKey entityKeyMed = _commonContext.CreateEntityKey("Medication", objMed);
objMed.EntityKey = entityKeyMed;
_commonContext.Attach(objMed);

objClient.Medication.Attach(objMed);
objClient.Medication.Remove(objMed); //this deletes
_commonContext.SaveChanges();

这是如何删除而不首先加载任何数据。这在EF5中有效。不确定早期版本

var db = new TopicDBEntities();

var topic = new Topic { TopicId = 1 };
var 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(); 

如果设置了外键,在删除父实体时,引用完整性应通过DBMS本身自动执行

如果您先使用代码,正如我在MVA教程中了解到的那样,“删除级联”上的是EF6设置的默认行为。如果先运行DB,您应该更改您的子表

以下是链接: 在视频中20:00向上提到,在幻灯片演示中第14页上提到


干杯,这正是我需要的。非常感谢。是否有任何方法可以在不必先加载订阅的情况下删除订阅?谢谢