Using Visual Studio 2017, C#, Entity Framework .Net 6.2.0
I have a setup where a table (LOG
) has a foreign key to another table (BAR
). I want to delete an entry from BAR
while leaving the entry and foreign key untouched in LOG
.
public class LOG
{
[Key]
public int id { get; set; }
public string statusLog { get; set; }
public virtual BAR bar { get; set; }
}
public class BAR
{
[Key]
public int id { get; set; }
public string data { get; set; }
}
I then attempt to delete an entry.
BAR bar1 = DBContext.BARs.Where(b => b.id == enteredID).First();
DBContext.BARs.Remove(bar1);
DB.Context.SaveChanges();
and get this exception
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.LOG_dbo.BARs_BAR_id". The conflict occurred in database \"*******\", table "dbo.LOG", column 'BAR_id'.
The statement has been terminated
How can I remove an entry from the BAR
table while leaving the entries in the LOG
table? I want the data to show what happened in my logging table even as entries are added and removed from my system.