0

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

DATABASE
enter image description here

DATABASE DESIGN enter image description here

ERROR 1
enter image description here

ERROR 2
enter image description here

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. enter image description here

Roopan Jaulin
  • 49
  • 1
  • 12

1 Answers1

1

You cannot update any column to auto increment from front end using query.

You have 3 options:

Option 1:-
Either update it using SQL mangement studio.

enter image description here

Option 2:- Drop your column and create new one. If there is no record in your current table. But this will change the ordinal position of your column.

-- ID is the name of the  [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID 
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)

Option 3:

You may drop your current table and create it again with the column set as IDENTITY


CREATE TABLE [dbo].[table](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    ...
    CONSTRAINT [PK_table] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

DarkRob
  • 3,843
  • 1
  • 10
  • 27
  • I tried your solution but getting another error. Please see update above. Thank you for your help. – Roopan Jaulin Aug 22 '19 at 10:42
  • 1
    seems like you are inserting value in `id` column, which is not required as it is auto generated. No need to set id for new inserted row. – DarkRob Aug 22 '19 at 10:46
  • Yes, you are right. But the thing is I do not know where this value is being inserted as I have just been given this project to handle. Is there any way I can track it down? – Roopan Jaulin Aug 22 '19 at 10:57
  • please find this link for reference https://stackoverflow.com/questions/8835434/insert-data-using-entity-framework-model. – DarkRob Aug 22 '19 at 10:59