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 6172871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:28:02+00:00 2026-05-23T23:28:02+00:00

We have a database with a tables called ‘Sites’ This table has the columns,

  • 0

We have a database with a tables called ‘Sites’
This table has the columns, SiteID, Name, Tags, Description, URI, with SiteID being a primary key (It is not set as an Identity because we want to add our own ID)

We have been using .NET 4.0 MVC with C# and have setup everything up in the code which we need.
We can select things from the database and display them so we know that is working.
But when we try to insert we get a Cannot insert the value NULL into column 'SiteID' error.

If I set the column as an Identity so that it auto generates, or if I take off the primary key then it is fine, but as I said it should be a primary key and we want to insert out own ID’s.

My code is below (We get the error on SaveChanges() but have checked in the debugger and SiteID is definitely being assigned an int)

Sites

public class Sites
{
    [Key]
    public int SiteID { get; set; }
    public string Name { get; set; }
    public string Tags { get; set; }
    public string Description { get; set; }
    public string URI { get; set; }
}

CMSModels

public class CMSModels : DbContext
{
//public DbSet<ContentTypeModel> ContentType { get; set; }
    //public DbSet<LayoutModel> Layout { get; set; }
    //public DbSet<PageModel> Page { get; set; }
    //public DbSet<PageZoneModel> PageZone { get; set; }
    public DbSet<Sites> Site { get; set; }
    //public DbSet<ZoneContentModel> ZoneContent { get; set; }
    //public DbSet<ZoneTypeModel> ZoneType { get; set; }
}

HomeController:

private CMSModels models = new CMSModels();

public ActionResult Index()
{
    Sites site = new Sites { SiteID = 4, Name = "Name", Description = "Desc", Tags = "", URI = "" };

    models.Site.Add(site);
    models.SaveChanges();

    return View(models.Site.ToList());
}

I don’t understand why I am getting this error, so any ideas would be appreciated.

If you need to see any other code please let me know.

Edit:

Just to extend this question a bit, it seems that this only happens on primary keys which are of the type int.

If we set the database column data type to nvarchar and the data types in the code to string it works fine.

Is this just a bug in Entity Framework 4, or are we just doing something wrong?

If we can’t find a way to insert ints into a primary key column we may just have to use strings instead and validate them to make sure they are numeric.

  • 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-05-23T23:28:03+00:00Added an answer on May 23, 2026 at 11:28 pm

    If you want a NO IDENTITY primary key column, so you set the id manually, you have 2 possible solutions.

    1. Decorate your id propertie with the following Attributes (DatabaseGenerated…).

        public class Sites
        {
            [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int SiteID { get; set; }
            public string Name { get; set; }
            public string Tags { get; set; }
            public string Description { get; set; }
            public string URI { get; set; }
        }
    

    2. Do it using the ModelBuilder in your DbContext.

        public class CMSModels : DbContext
        {
          //public DbSet<ContentTypeModel> ContentType { get; set; }
          //public DbSet<LayoutModel> Layout { get; set; }
          //public DbSet<PageModel> Page { get; set; }
          //public DbSet<PageZoneModel> PageZone { get; set; }
          public DbSet<Sites> Site { get; set; }
          //public DbSet<ZoneContentModel> ZoneContent { get; set; }
          //public DbSet<ZoneTypeModel> ZoneType { get; set; }
    
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
              modelBuilder.Entity<Sites>().Property(r => r.SiteID) 
                           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a database with two tables called Articles and Tags . In
I have two tables in my database, called ratings and movies . Ratings: |
I have a couple of tables in my database, one called Task , and
I have a database table called Posts which stores all the information regarding an
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
Lets say I have a table in a sql server 2000 database called TransactionType:
I have database with many tables. In the first table, I have a field
I have 2 database tables: Teams and Games. For the purpose of this question,
I have 2 related database tables which in simplified form look like this Product(
I have a table called WorkItemNotes, and separate tables for Customers and Employees. I

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.