Flenner Tom

Flenner Tom

  • NA
  • 9
  • 516

Problem with Many to Many and EF6

Mar 28 2019 3:53 PM
Hello guys!
 
I have some trouble with EF6 using MySql database and a many to many relation with an association table.
 
So i have these entities :
  1. [Table("user")]  
  2. public partial class user  
  3. {  
  4. public user()  
  5. {  
  6. }  
  7. public int id { getset; }  
  8. [Required]  
  9. [StringLength(255)]  
  10. public string first_name { getset; }  
  11. [Required]  
  12. [StringLength(255)]  
  13. public string last_name { getset; }  
  14. [Required]  
  15. [StringLength(255)]  
  16. public string email { getset; }  
  17. [Required]  
  18. [StringLength(255)]  
  19. public string hash { getset; }  
  20. public virtual ICollection<role> Roles { getset; }  
  21. [Table("role")]  
  22. public partial class role  
  23. {  
  24. public role()  
  25. {  
  26. }  
  27. public int id { getset; }  
  28. [Required]  
  29. [StringLength(255)]  
  30. public string title { getset; }  
  31. public virtual ICollection<user> Users { getset; }  
  32. }  
And i start like that : i enable migrations, its working perfectly. I add first migration, its working perfectly.
 
But when i try to update-database i have this error :
 
System.FormatException : Input string was not in a correct format.
 
But i dont know why, because when i delete the collection in the 2 entities it work perfectly..
There is my context :
  1. public class TestContext : DbContext  
  2. {  
  3. public TestContext() : base("name=TestContext")  
  4. {  
  5. Database.SetInitializer<TestContext>(new DropCreateDatabaseIfModelChanges<TestContext>());  
  6. }  
  7. public virtual DbSet<role> Roles { getset; }  
  8. public virtual DbSet<user> Users { getset; }  
  9. protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  10. {  
  11. modelBuilder.Entity<user>()  
  12. .HasMany(e => e.Roles)  
  13. .WithMany(e => e.Users)  
  14. .Map(m => m.ToTable("role_user""test"));  
  15. }  
Someone have idea ?
 
Thank you!

Answers (5)