0

Consider a Course can has a Curriculum or not. So the relation between the Course and Curriculum is 1..0 or 1..1. The classes are:

public class Course
{
    public int Id {get; set;}
    public Curriculum { get; set; }
}


public class Curriculum 
{ 
    [ForeignKey("Course")]
    public int Id { get; set; }     

    public Course Course { get; set; }     
}

I configured the relation by fluent api like below:

public class CourseConfiguration : EntityTypeConfiguration<Course>
{
    public CourseConfiguration()
    {
        HasOptional(course => course.Curriculum)
            .WithRequired(cur => cur.Course);
    }
}

unfortunately, when i want to save a course in the Db this error is raised:

Cannot insert explicit value for identity column in table 'Curricula' when IDENTITY_INSERT is set to OFF

Additionally, i added attributes [DatabaseGenerated(DatabaseGeneratedOption.Identity)] or [DatabaseGenerated(DatabaseGeneratedOption.None)] to the primary key of Curriculum class, but it has no effect or creates new exception!

Do you have any idea?

Ehsan Toghian
  • 548
  • 1
  • 6
  • 26

1 Answers1

0

I took your code and it worked:

> public class MyContext : DbContext
>     {
>         public DbSet<Curriculum> Curriculum { get; set; }
>         public DbSet<Course> Course { get; set; }
> 
>         protected override void OnModelCreating(DbModelBuilder modelBuilder)
>         {
>             base.OnModelCreating(modelBuilder);
>             modelBuilder.Configurations.Add(new CourseConfiguration());
>         }
>     }
>     public class Course
>     {
>         [DatabaseGenerated(DatabaseGeneratedOption.None)]
>         public int Id { get; set; }
>         public Curriculum Curriculum { get; set; }
>     }
> 
>     public class CourseConfiguration : EntityTypeConfiguration<Course>
>     {
>         public CourseConfiguration()
>         {
>             HasOptional(course => course.Curriculum)
>                 .WithRequired(cur => cur.Course);
>         }
>     }
>     public class Curriculum
>     {
>         [ForeignKey("Course")]
>         public int Id { get; set; }
> 
>         public Course Course { get; set; }
>     }

However, in your case, since the the table was first created with identity set to true, EF wouldn't change it for you. If you had had the following attribute on the Id property of Course in your first migration, the problem wouldn't have occured:

[DatabaseGenerated(DatabaseGeneratedOption.None)]

In order to fix your problem, you need to recreate the table Course. The following link can also help you: https://stackoverflow.com/a/18917348/2348125

Amir Molaei
  • 3,700
  • 1
  • 17
  • 20
  • Thanks @rad, but id **Id** in the Course class is actually Identity and changing it to the `None` causes a great impact on other classes and tables. – Ehsan Toghian Feb 17 '19 at 08:44
  • Sorry, I confused Curriculum with course. Do the same for Curriculum (recreate table) and check the result. If it didn't work please add the code that does the insertion to your question. – Amir Molaei Feb 17 '19 at 09:37
  • I tried it for Curriculum table, but because of many relation between Curriculum table and other tables, updating the db produces a lot of errors. Additionally, i don't want to manually handle the Curriculum's IDs. I can't understand the reason, but i have implemented entities with similar structures and relations many times. – Ehsan Toghian Feb 17 '19 at 11:56
  • The error indicates that you are trying to insert a record into the table with an explicit value in a column whose identity is set to true. So if that's what you desire, then the problem might be from the insertion statement where you are possibly trying to insert explicit value for Id. – Amir Molaei Feb 17 '19 at 15:05