Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net Core2 EF MySQL having issue while changing foreign key column to nullable #483

Closed
SharmaHarsh7 opened this issue Feb 8, 2018 · 4 comments

Comments

@SharmaHarsh7
Copy link

SharmaHarsh7 commented Feb 8, 2018

I am working on an application where I am using .Net Core 2, EF Core and MySQL as database server via Code First approach.

I have 2 tables:

  1. User
  2. Employee

User table is the main table which contains the user information and Employee table is the child table which has a column ID_User as shown below:

 public class User : BaseEntity
    {
        public int ID_User { get; set; }
        public string Name { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public virtual ICollection<Employee> Employees{get;set;}
    }



 public class Employee : Entity
    {
        public int ID_Employee { get; set; }
        public string Name { get; set; }

        public int ID_User { get; set; }

        public virtual User User { get; set; }
    }

Everythihg works perfectly when I use the above mapping and I have enaugh data in both the tables.

Now, I want to make the column ID_User in Employee table as nullable

To implement this change I made folloeing change to my model:

public class Employee : Entity
    {
        public int ID_Employee { get; set; }
        public string Name { get; set; }

        public int? ID_User { get; set; }

        public virtual User User { get; set; }
    }

and in mapping file:

builder.HasOne(x=>x.User).WithMany(y=>y.Employees).HasForeignKey(z=>z.ID_User).IsRequired(false);

After running the dotnet ef migrations add empuser command it generated the following migration code:

migrationBuilder.DropForeignKey(
name: "FK_Employee_User_ID_User",
table: "Employee");

    migrationBuilder.AlterColumn<int>(
        name: "ID_User",
        table: "Employee",
        nullable: true,
        oldClrType: typeof(int));

    migrationBuilder.AddForeignKey(
        name: "FK_Employee_User_ID_User",
        table: "Employee",
        column: "ID_User",
        principalTable: "User",
        principalColumn: "ID_User",
        onDelete: ReferentialAction.Restrict);

Now when I run dotnet ef database update it is giving me the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT FK_Employee_User_ID_User' at line 1

Please help.

Thanks

@mguinness
Copy link
Collaborator

Can you generate the SQL? It appears that it's a SQL syntax error so that should provide some clues.

@SharmaHarsh7
Copy link
Author

SharmaHarsh7 commented Feb 9, 2018 via email

@caleblloyd
Copy link
Contributor

Turn "Microsoft" logging to "Information" then run your migrations and it will log SQL. Example (change Microsoft: error to Microsoft: information)

https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/master/test/EFCore.MySql.FunctionalTests/appsettings.json

@SharmaHarsh7
Copy link
Author

@caleblloyd, Thanks man I got this fix.

Actually I was using MySQL official connector for this which was causing the issue.

Later I tried same scenario with Pomelo.EntityFrameworkCore.MySql and it worked exactly same as expected.

!!! Pomelo Rocks !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants