To delete a row from a table of only foreign keys using Entity Framework c# entity-framework-6 linq


The table I haveUser { UserId, Name, Age } and License { LicenseId, Name, IsActive } . I have a table as well.UserLicense { UserId, LicenseId } This links the two of them and maintains a list of all licence holders. Users who own multiple licences have separate rows in theUserLicense table.

I must now delete a specific licence from the licencing table. Since I do not want to completely destroy it, I markIsActive = false . Nevertheless, I would want to remove rows from theUserLicense for that licence ID's table.

Use of Entity Framework is mine.

If the table were straight, I would have done something like this:

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

though, sinceUserLicense Entity Framework prevents me from accessing a database with foreign keys directly using

public void DeleteLicensedUsers(Guid LicenseId)

because UserLicense is merely a table of foreign keys in the model and does not have its own standalone table.

How then can I remove each row fromUserLicense table for a certainlicenseId EF6 and Linq together?

6/12/2018 5:34:41 PM

Accepted Answer

You may resolve this dilemma in the same manner that I did. In order to address this problem, I nearly always add an additional column to tables that only contain foreign keys.

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

if (users != null)
    foreach (var user in users)
6/13/2018 1:46:58 PM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow