0

I get a slightly wierd exception when i try to add an object to my database. My code in my controller looks like this:

var user = db.UserActivation.Create();
user.UserId = Id
user.ActivationCode = activationCode;
db.UserActivation.Add(user);
db.SaveChanges();

Furthermore i have a model that looks like this:

public class UserActivation
{
    [Key]
    public int Id { get; set; }
    public int UserId { get; set; }
    public string ActivationCode { get; set; }
}

and a MySqlContext that looks like this:

public class MySqlContext : DbContext
{
    public MySqlContext() : base("MySql")
    {
        //this.Configuration.LazyLoadingEnabled = false;
    }
    public DbSet<Users> Users { get; set; }
    public DbSet<UserActivation> UserActivation { get; set; }
}

In my error i can see in the inner exception, that it selects the wrong database, it says:

[MySql.Data.MySqlClient.MySqlException {"Table 'bkrt_dk_db.UserActivations' doesn't exist"}

This makes absolutely no sense, since i both in my model, context and controller specifically have called it UserActivation and NOT UserActivations

This is my stacktrace:

at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Learning.Controllers.AuthController.emailConirmation(String email, String name) in c:\Users\Projects\Learning\Learning\Controllers\AuthController.cs:line 146
at Learning.Controllers.AuthController.<>c__DisplayClass4.<Registration>b__2() in c:\Users\Projects\Learning\Learning\Controllers\AuthController.cs:line 107
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

I can see that others have stated that it has something to do with pluralization, and EF making the db architecture behind the scenes.. but really i dont know about pluralization and why i get this error.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Jeppe Christensen
  • 1,680
  • 2
  • 21
  • 50
  • Possible duplicate of [How turn off pluralize table creation for Entity Framework 5?](http://stackoverflow.com/questions/12130059/how-turn-off-pluralize-table-creation-for-entity-framework-5) – mason Mar 09 '17 at 15:55
  • its not a duplicate. read the question again. Your suggestion doesn't explain why this error occurs. – Jeppe Christensen Mar 09 '17 at 15:59
  • So if you don't understand the duplicate, why make the statement that it's not a duplicate? EF by default tries to pluralize the table names. Since your table name isn't pluralized, you need to tell EF not to do that. Hence why I provided that link for you. – mason Mar 09 '17 at 16:03
  • Thats great, but it will discredit my question. Im new to this, and a line of code doesn't help me understand and hence solve this issue, if i run into this same error in the future. - I don't understand why it would even pluralize my name in the first place, since i specifically have stated the name. – Jeppe Christensen Mar 09 '17 at 16:05
  • I don't see how it would "discredit" your question. As I just pointed out, EF by default tries to pluralize the name. That's just how it works. So if the name of your table isn't pluralized, it will be a mismatch and you'll get the error you received. You can either change the name of your table to match, or you can tell EF not to pluralize the name. – mason Mar 09 '17 at 16:07
  • The purpose of flagging as a duplicate is to point you toward an existing answer that will solve your problem, not discredit your question. Please keep an open mind toward people that are trying to help you for free. – Seano666 Mar 09 '17 at 18:39
  • apologies, and i appreciate the help a lot, i really do. - I just feel that this forum at times can be frustrating when you are new to e.g. a framework and try to get help. Often things are not explained thoroughly, and it consequently means that i miss fundamental things upon getting better at this. For example i still dont know why EF want to pluralize db tables, and why it could be useful, other than "thats just how it is". – Jeppe Christensen Mar 09 '17 at 19:12

0 Answers0