INSERT fails on EF 4.1 Code First Database

Discussion in 'Databases' started by jabarkas, May 26, 2011.

  1. I am using Visual Studio 2010 SP1 with MVC3 Tools Update, and EF 4.1 Code First to scaffold the database.

    CRUD operations work fine when testing locally and with a local database, but once I migrate the database to Winhost my INSERT operations fail (I get this error: Cannot insert the value NULL into column 'Id', table 'DB_23378_bloomlmsdata.dbo.Courses'; column does not allow nulls. INSERT fails. The statement has been terminated.)

    I read somewhere that Entity Framework, when performing an INSERT operation to the database it will first try to insert a record with a null value as the Primary Key. I do not know how to override this behaviour. Also, this does not seem to be a common problem and people usually only run in to this when they use something different than an IDENTITY key.

    Has anybody had success with EF 4.1 Code First scaffolded database on Winhost? I would be open to suggestions to get this working. However, after some research I feel that this is not a common problem and that EF 4.1 Code First does not work on Winhost (For the record, I had the same problem with EF CodeFirst CTP5 before EF 4.1 was released).


    My data models all specify a Primary Identity Key like so:

    [Key]
    public int Id {get; set;}
    ...

    And in the local database that gets scaffolded, I see a non-nullable Primary Identity Key in each table.

    I have been troubleshooting this for 4 weeks.. any ideas would be greatly appreciated.
     
    Last edited by a moderator: Oct 14, 2015
  2. You seem to have a very good grasp of the issue. Im not sure how much I can help, however, I would like to ask a couple questions:

    A - Are there already some records in the DB from when you ran it on your local computer or is it empty?

    B - I have not seen this. My best suggestion would be to try to make a small simple version. New app, new DB, etc. But with just an ID and second field. Then try to run it live. See if you get the same error as you do with your "real application". This will either confirm your theory or disprove it. Then you will know what direction to start looking in.
     
  3. Yes, there are some records in there already from my local version of the db.

    Making a small test is a good idea and probably something I have been putting off. I will let you know how it goes.

    Thanks
     
  4. Please do, I will keep up with this thread and provide any help I can.
     

Share This Page