Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Foreign Key based on another column other than Id ef core model first

$
0
0

I have a table that I am using for depended dropdowns but I am having an issue I am getting a key violation on my table because I am using a secondary column to group my dropdowns.

public void GetEnforcementCats() {

List<SelectListItem> list = new List<SelectListItem>();
var items = _context.EnfCats.Where(w => w.isActive==true && w.isDeleted==false).ToList();
foreach (var item in items) {
list.Add(new SelectListItem { Text = item.Description.ToString(), Value = item.CategoryId.ToString() });
}
ViewBag.EnforcementCats = list;
}

But of course when I look in the table of ef cats and then try to save against that in the error that I get is the following.

DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_MISobject_EnfCats_Enf_Cat". The conflict occurred in database "MISSystem", table "dbo.EnfCats", column 'Id'. The statement has been terminated.

Or should I just do away with the foreign key in this case and just save the int Value ?

 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>