none
Trouble inserting data using Code first RRS feed

  • Question

  • Hi,

    I am using codefirst with an existing database(School database schema from msdn) and RIAServices.

    I tried the following to add a course (CourseID is Key but not identity)

     

    SchoolContext ctx = new SchoolContext

    ();

    Course c = new Course { CourseID = 11, Credits = 3, DepartmentID = 7, Title = "A New Course" };

    ctx.Courses.Add(c);

    ctx.SaveChanges();

    Not sure what the problem is, I get the following error

    "Cannot insert the value NULL into column 'CourseID', table 'School.dbo.Course'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."

    

    

    [

    Table("Department")]

    public class

    Department

    {

    [

    Key

    ]

     

    public int DepartmentID { get; set

    ; }

     

    public string Name { get; set

    ; }

     

    public decimal Budget { get; set

    ; }

     

    public DateTime StartDate { get; set

    ; }

     

    public int Administrator { get; set

    ; }

    }

    [

    Table("Course"

    )]

     

    public class

    Course

    {

    [

    Key

    ]

     

    public int CourseID { get; set

    ; }

     

    public string Title { get; set

    ; }

     

    public int Credits { get; set

    ; }

     

    public int DepartmentID { get; set

    ; }

    [

    Association("aa","DepartmentID","DepartmentID"

    )]

    [

    Include

    ]

     

    public virtual Department Department { get; set

    ; }

    }

     

    public class SchoolContext :

    DbContext

    {

     

    public DbSet<Department> Departments { get; set

    ; }

     

    public DbSet<Course> Courses { get; set

    ; }

     

    protected override void OnModelCreating(DbModelBuilder

    modelBuilder)

    {

    modelBuilder.Entity<

    Course

    >().HasRequired(c => c.Department).WithMany().HasForeignKey(c => c.DepartmentID);

    }

    }


    http://leeontech.wordpress.com/
    Wednesday, July 13, 2011 7:04 PM

Answers

  • Hi Lee,

    I recently had the same error. I am not having that project at hand, but as far as I remember, the poblem was that the database model has the (primary key field) Id set as Identity. In the model, I clicked the Id field and for that field in the properties, I changed the property StoreGeneratedPattern from Identity to none. Regenerated the database and it worked.

    To the community: I beleive that this maybe is bad practice to switch of Identity for the primary key field. Better solutions would be welcome (then I can learn some more too)

    • Marked as answer by lee d Thursday, July 14, 2011 6:30 PM
    Thursday, July 14, 2011 6:04 AM

All replies

  •  Hi Lee;

    I believe that this line of code :

    ctx.Courses.Add(c);

    Should be this :

    ctx.Courses.AddObject(c);

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, July 13, 2011 8:14 PM
  • Hi,

    When I changed to AddObject it says

    Error 7 'System.Data.Entity.DbSet<Sla2.Web.Models.Course>' does not contain a definition for 'AddObject' and no extension method 'AddObject' accepting a first argument of type 'System.Data.Entity.DbSet<Sla2.Web.Models.Course>' could be found (are you missing a using directive or an assembly reference?) 


    http://leeontech.wordpress.com/
    Wednesday, July 13, 2011 9:21 PM
  • Hi Lee;

    Well I don't understand why you can not see the AddObject method. As can be seen from the Class definition at this link

    ObjectContext Class

    You can see that it is part of the Object Context and if you look at this link you can see the AddObject defined and a example.

    ObjectContext.AddObject Method

    Are you sure you have the correct reference to the System.Data.Entity.dll in the project that you are getting the error?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, July 14, 2011 2:33 AM
  • Hi lee,

    Welcome!

    I think you can pass Department instance instead of its Id.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    namespace ConsoleApplication44
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (var context= new SchoolContext())
          {
            //context.Database.CreateIfNotExists();
            Department d = context.Departments.Find(1);
            Course c = new Course { CourseID = 12, Credits = 3, Department=d, Title = "A New Course" };
             context.Courses.Add(c);
            context.SaveChanges();
          }
        }
      }
    
      [Table("Department")]
      public class
      Department
      {
        [Key]
        public int DepartmentID
        {
          get;
          set;
        }
     public string Name
        {
          get;
          set;
        }
     public decimal Budget
        {
          get;
          set;
        }
     public DateTime StartDate
        {
          get;
          set;
        }
     public int Administrator
        {
          get;
          set;
        }
      }
      [Table("Course")]
      public class
      Course
      {
        [Key]
    public int CourseID
        {
          get;
          set;
        }
     public string Title
        {
          get;
          set;
        }
    public int Credits
        {
          get;
          set;
        }
     public int DepartmentID
        {
          get;
          set;
        }
    
     [Association("aa", "DepartmentID", "DepartmentID")]
       public virtual Department Department
        {
          get;
          set;
        }
      }
     public class SchoolContext :DbContext
      {
     public DbSet<Department> Departments
        {
          get;
          set;
        }
     public DbSet<Course> Courses
        {
          get;
          set;
        }
    protected override void OnModelCreating(DbModelBuilder
    
        modelBuilder)
        {
          modelBuilder.Entity<Course>().HasRequired(c => c.Department).WithMany().HasForeignKey(c => c.DepartmentID);
        }
      }
    }
    
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 3:39 AM
    Moderator
  • Hi Lee,

    I recently had the same error. I am not having that project at hand, but as far as I remember, the poblem was that the database model has the (primary key field) Id set as Identity. In the model, I clicked the Id field and for that field in the properties, I changed the property StoreGeneratedPattern from Identity to none. Regenerated the database and it worked.

    To the community: I beleive that this maybe is bad practice to switch of Identity for the primary key field. Better solutions would be welcome (then I can learn some more too)

    • Marked as answer by lee d Thursday, July 14, 2011 6:30 PM
    Thursday, July 14, 2011 6:04 AM
  • Alan,

    I tried that, it did not make a difference, moreover the error is about the CourseID and not department.

     

    Bart,

    I was initially not setting the CourseID(assuming it would be a identity). but when I looked in the Database it was not Identity column. so added code to set that).

     

    Not sure if this is what the issue is, but I am using NuGet (http://nuget.org/List/Packages/RIAServices.EntityFramework)


    http://leeontech.wordpress.com/
    Thursday, July 14, 2011 7:13 AM
  • Hi lee,

    I used this http://nuget.org/List/Packages/EntityFramework to include the entityframework.dll.

    I'm not familiar with RIAservices, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 11:24 AM
    Moderator