3

I have this entity in my EF code First

 public class Equipment
    {
        public Int64 Id { set; get; }
        public string Name { set; get; }
        public string Model { set; get; }
        public string Factory { set; get; }
        public DateTime SubmitDateTime { set; get; }
        public GUID OrganizationId { set; get; }
    }

I changed it to

 public class Equipment
    {
        public Int64 Id { set; get; }
        public string Name { set; get; }
        public string Model { set; get; }
        public string Factory { set; get; }
        public DateTime SubmitDateTime { set; get; }
        public Int64 OrganizationId { set; get; }
    }

but after running i get this error:

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Operand type clash: uniqueidentifier is incompatible with bigint

here my migration method:

 public class MigrationsConfiguration : DbMigrationsConfiguration<DataContext>
    {

        public MigrationsConfiguration()
        {
            this.AutomaticMigrationDataLossAllowed = true;
            this.AutomaticMigrationsEnabled = true;
        }



    }

I have lots of data in my Db .So i can't remove it to generate it again.so how can i fix this error ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • Do you have data in your Organization table? You cannot do that and the error is clear. The Organization table has GUID as primary key and the OrganizationId in Equipment is referencing it. Now you are changing it to bigint and it has no idea what to do. – CodingYoshi Jun 24 '17 at 16:36
  • One simple method for arbitrary changes is to just start over with a new database. Let EF create the tables, and then load them from your old database. – David Browne - Microsoft Jun 24 '17 at 16:40
  • You need to change the OrganizationId column in Organization table firstly, populate them with bigints and then update your Equipment table so it is using the new values. You will need to stage the table, drop the foreign key etc. – CodingYoshi Jun 24 '17 at 16:43
  • @DavidBrowne-Microsoft as i said i have lots of data in my db – Ehsan Akbar Jun 24 '17 at 16:43
  • Are you comfortable with writing SQL script? – CodingYoshi Jun 24 '17 at 16:45

1 Answers1

3

With three steps i changed it : first I added another column to my entity

public GUID OrganizationId { set; get; }
public int64 OrganizationId2 { set; get; }

second I removed the guid column in my entity

public int64 OrganizationId2 { set; get; }

last step finally i renamed my new column

public int64 OrganizationId { set; get; }
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • 1
    This is why I asked you if your are ok with SQL script. I was about to suggest the same but with an additional step: You need to populate the column in the parent table and also the child table. You did not do that and since you have data, that is a very important step. – CodingYoshi Jun 24 '17 at 16:46
  • 1
    Such a pragmatical approach, I couldn't expect such a basic solution lol. Thanks! – aozan88 Feb 04 '22 at 10:00