I have a table User { UserId, Name, Age }
and 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. A user can hold multiple license and those appear as different rows in the UserLicense
table.
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
. However I do want to delete rows from the UserLicense
table for that license 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
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.
So how do I delete all rows of UserLicense
table for a particular licenseId
using Linq and EF6?