1

I have 2 entities Course and Group which are many-many related. I am using code first migrations. when i update-database -v it is throwing an exception I am aware that Many-Many relationship creates another table GroupCourses.

StackTrace : ALTER TABLE [dbo].[GroupCourses] ALTER COLUMN [Course_CourseId] [int] NOT NULL

Exception : Operand type clash: uniqueidentifier is incompatible with int

    public class Course
    {
        [Key]
        public int CourseId { get; set; }

        public virtual ICollection<Group> Groups { get; set; }
    }

    public class Group
    {
        [Key]
        public Guid groupId { get; set; }

        public virtual ICollection<Course> Courses{ get; set; }
    }

I think it is due to different PK types in both entities. Am i correct? If yes, is there any solution to overcome this.

Thanks in Advance.

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90

1 Answers1

0

You cannot update a primary key with Entity Framework.

When you tried to update, I'm assuming PK CourseId from a Guid to an int, this is what is caused the issue (not the PKs being different types in the associative table, this is fine).

Either drop the table or change the type manually.

Community
  • 1
  • 1
chandler
  • 1,134
  • 1
  • 17
  • 33
  • I am getting exception when i try to generate `database` from the `models`. When i run the command `update-database -v` on powershell to create database. then i am getting exception. – Venkata Dorisala Jun 24 '16 at 15:52
  • I dropped all the tables. Created new `migration` with latest models and run the command `update-database -v` . Still the same error. – Venkata Dorisala Jun 24 '16 at 15:54