So I'm having some issues with my code here as I am new to C# and linq. I'm trying to insert id, icon_url and count values into my database. But when I'm inserting the first row, the id is set as 0 and when I add another row, I'm getting two errors. I want to auto increment the ID as it is primary key and is of type int. How can I fix this? Please see codes and screenshots below.
IMAGES
CODE:
bool imcount = db.dashboards.Any(dash => dash.icon_url.Contains(imageurl));
if (imcount == false)
{
using (ECardModel db = new ECardModel())
{
dashboard imageCount = new dashboard()
{
icon_url = imageurl,
count = 1
};
db.dashboards.Add(imageCount);
db.SaveChanges();
}
}
else if (imcount == true)
{
using (ECardModel db = new ECardModel())
{
int icount = db.dashboards.Where(dash => dash.icon_url.Contains(imageurl)).Max(dash => dash.count);
dashboard imageCount = new dashboard()
{
count = icount + 1
};
db.dashboards.Add(imageCount);
db.SaveChanges();
}
}
Update:
When I set identity specification to "Yes", it is giving me another error.
Please note that i am unable to update my .edmx model as it is in xml format and not showing model diagrams. Also, the number 0 is still inserted in the database.
Please see image below.