Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:20:30+00:00 2026-06-12T08:20:30+00:00

With MVC4, I’m having some trouble inserting new rows into my database (SQL Compact).

  • 0

With MVC4, I’m having some trouble inserting new rows into my database (SQL Compact). I’m building the rows from raw text (tab separated) like this:

                while (reader.Peek() != -1)
            {
                newRow = Cos.Create();
                line = reader.ReadLine().Split('\t');
                for (int i = 0; i < header.Count(); i++)
                {
                    switch (header[i])
                    {
                        case "CompanyName":
                            newRow.CompanyName = line[i];
                            break;
                        case "City":
                            newRow.City = line[i];
                            break;
                        case "Country":
                            newRow.Country = line[i];
                            break;
                        default:
                            return "A column was found with an unacceptable value - " + header[i] + " - No changes have been made.";
                    }
                }
                this.Cos.Add(newRow);
            }
        this.SaveChanges();

My table has an Id column, which is set to be: int, Unique, Primary Key, and Identity (seed:1, increment:1). The problem is that SQL rejects the statements generated by “this.SaveChanges()” because the Id values of all the rows I generate are 0.

Is there a way for me to have SQL assign a unique Id to each row upon insertion? It was my understanding that this way the purpose of Identities.

My model simply looks like this:

public class CoDB{
        public string CompanyName { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public int Id { get; set; }
}

And CoDBContext defines:

public DbSet<CoDB> Cos {get; set;}

Thank you so much for any help!
-Zach

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:20:32+00:00Added an answer on June 12, 2026 at 8:20 am

    Sounds like your trying to set the value of an auto identity field, which you shouldn’t. See if this helps: Autonumber with Entity Framework

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a POC using MVC4. I have some jquery json calls to
New to SPA MVC4, trying to pass a session variable to LinqToEntitiesDataController from the
I recently updated from MVC4 beta to the RC and have encountered a small
In my MVC4 app, I'm trying to create a client side validator for some
I am creating a MVC4 application and I have got some troubles when I
I am using the new mvc4 ajax controller template and I don't know how
I noticed when the MVC4 beta was announced, it includes a new Web-API .
I'm writing some custom Html Helpers for my MVC3 and soon MVC4 project. Examples
I have an MVC4 application with an aspnet.mdf database with membership tables and I’ve
In MVC4, if I create a new build configuration for all projects in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.