2

I'm working on an ASP MVC project.

Here are the steps I followed :

  • I added entity framework in my project references
  • I connected to my SQL SERVER database and then copied the associated connection string in Web.config using this answer. The connection is successful

  • I created manually my own DbContex class. Below is its code :

    public class MyConxtext : DbContext
    {
        public MyConxtext() : base("name = MyConnString"){}
    
        public DbSet<user> user { get; set; }
    }
    

Now user here not only is the name of my table user in SQL server but also is the name of my model user in my ASP MVC.

My problem is that : - when I wanted to persist (several item in a session) via MyContext.SaveChanges(), it has created another table in my database named users ... Notice the plural here users... So instead of working on the table user, it created another table called users and persited data on that table.

My context also is not able to read data from the user table. BUT as I said it processes everything is the schema of the connection string

How can I solve that problem ?

Not only that it has also created another table in my schema called MigrationHistory which contain data about my project...

Community
  • 1
  • 1
Bloomberg58
  • 157
  • 1
  • 19

2 Answers2

1

You can override OnModelCreating method in your DbContext and add the following line

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

This should remove pluralization for naming

Random
  • 3,807
  • 2
  • 30
  • 49
  • I have no idea why this helped me. But it seems to have solved the problem. I have deleted MigrationHistory according to http://stackoverflow.com/a/37488834/7885071 and added your line to override the method. Problem solved. If you have time please elaborate on what exactly happened and why you gave that solution. ANYWAY THANK YOU MATE – Bloomberg58 Apr 19 '17 at 15:41
  • It seems you have automatic migration enabled and by default EF uses pluralized table names. This option switches off this pluralization so that your model matches your existing schema – Random Apr 19 '17 at 16:16
0

Entity Framework by default pluralizes the table name when it generates it from the supplied entities.That means, if there is a class named Student as entity object and you declare a DbSet <Student> on your context (class inherited from DbContext), by default EF creates the table in a plural form of the entity name. This is a general concept as the table will contain several rows of that entity type, which refers it will be a collection of the entities, in our case it will make a table named Students.

If you want to remove this default convention (System.Data.Entity.ModelConfiguration.Conventions), you can override the method named

onModelCreating()

of DbContext class.

In your code you can do

public class MyConxtext : DbContext
{
    public MyConxtext() : base("name = MyConnString"){}

    public DbSet<user> user { get; set; }




    protected override void OnModelCreating(DbModelBuilder modelBuilder)  
    {  
        modelBuilder.Conventions.Remove < PluralizingTableNameConvention > ();  
    } 


 }

This will stop pluralizing the table's name in your database and it will use the user table of your database as you mentioned.

SouravOrii
  • 555
  • 9
  • 13
  • 1
    Awesome. Thank you mate. Your explanation made me better understand the code. By the way, I stuck on another problem...could you take a look at it http://stackoverflow.com/questions/43507903/why-is-my-microsoft-owin-security-authenticationmanager-signin-method-not-workin – Bloomberg58 Apr 20 '17 at 09:37