简体   繁体   English

使用实体框架从仅外键表中删除行

[英]To delete a row from a table of only foreign keys using Entity Framework

I have a table User { UserId, Name, Age } and License { LicenseId, Name, IsActive } . 我有一个表User { UserId, Name, Age }License { LicenseId, Name, IsActive } Also I have a table UserLicense { UserId, LicenseId } which connects both of these and has the record for all the users who hold a license. 另外,我还有一个表UserLicense { UserId, LicenseId }可以将这两者连接起来,并为持有许可证的所有用户提供记录。 A user can hold multiple license and those appear as different rows in the UserLicense table. 一个用户可以持有多个许可证,这些许可证在UserLicense表中显示为不同的行。

Now I need to remove a particular license from the license table. 现在,我需要从许可证表中删除特定的许可证。 I do not want to delete it outright, so I mark IsActive = false . 我不想直接删除它,因此我将IsActive = false标记IsActive = false However I do want to delete rows from the UserLicense table for that license Id. 但是,我确实想从UserLicense表中删除该许可证ID的行。

I am using Entity Framework. 我正在使用实体框架。

If it is a direct table, I would have done something like : 如果它是一个直接表,我会做类似的事情:

var lic = db.Licenses.Where(l => l.Id== licenseId).FirstorDefault();
db.Licenses.Remove(lic);
db.SaveChanges();

However since UserLicense is a table of foreign keys, Entity Framework does not allow me to access it directly using 但是,由于UserLicense是外键表,因此Entity Framework不允许我使用来直接访问它

public void DeleteLicensedUsers(Guid LicenseId)
{
    db.UserLicenses.Where()
}

because the model does not contain an independent table for UserLicense as it is only a table of foreign keys. 因为该模型不包含UserLicense的独立表,因为它只是外键表。

So how do I delete all rows of UserLicense table for a particular licenseId using Linq and EF6? 那么,如何使用Linq和UserLicense删除特定licenseIdUserLicense表的所​​有行?

If your entity class (I guess User ), has navigation properties of type ICollection<Licence> by removing Licence from this collection you are actually removing items from UserLicense table. 如果您的实体类(我猜是User )具有ICollection<Licence>类型的导航属性,则通过从此集合中删除许可证,实际上是从UserLicense表中删除项目。 This is how Entity Framework handles many-to-many relationships in databases. 这就是Entity Framework处理数据库中多对多关系的方式。

I faced a similar issue and you can solve it this way. 我遇到了类似的问题,您可以通过这种方式解决。 I almost tend to avoid tables with just foreign keys and instead add another column to fix this issue. 我几乎倾向于避免只使用外键的表,而是添加另一列来解决此问题。

var license = db.Licenses.Where(l => l.ID == LicenseId).FirstOrDefault();
var users = license.Users.ToList();


if (users != null)
{
    foreach (var user in users)
    {
         license.Users.Remove(user);
    }                   
}

I'm a little confused by your question. 你的问题让我有些困惑。 So if you mark a license as isActive = false , and still want to see that license appear on user's page, then there should still a record in UserLicense table associating the two. 因此,如果您将许可证标记为isActive = false ,并且仍然希望看到该许可证出现在用户页面上,则UserLicense表中仍应存在一条记录,将UserLicense关联起来。

That said, I believe this answer from a similar question will help you. 也就是说,我相信类似问题的答案将对您有所帮助。

so essentially: 所以本质上:

//include licenses in the user entity
var result = ctx.User.Include(x=>x.Licenses).Where(t=>t.Id == _id).FirstOrDefault()
//get the licenses you are looking for, either equal or use .Contains
var licensesOfUser = result.Where(x=>x.Licenses.Where(l=>l.id == _licenseId);
//remove them from entity and save.
result.Licenses.RemoveRange(licenseOfUser);
ctx.SaveChanges();

[edit: if you have license] [编辑:如果您有许可证]

//include users in license entities  
var result = ctx.License.Include(x=>x.Users).Where(t=>t.Id == _id).FirstOrDefault()
//get the users you are looking for, either equal or use .Contains
var usersOfLicenses = result.Where(x=>x.Users.Where(l=>l.id == _userId);
//remove them from entity and save.
result.Users.RemoveRange(licenseOfUser);
ctx.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM