Home  >  Article  >  Database  >  Why Does Entity Framework Create a Plural Table While My View Expects a Singular Table?

Why Does Entity Framework Create a Plural Table While My View Expects a Singular Table?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 10:29:30431browse

 Why Does Entity Framework Create a Plural Table While My View Expects a Singular Table?

Entity Framework Creates a Plural Table While View Expects a Singular Table

In the provided code, Entity Framework creates multiple tables even though the view requires a single table. This discrepancy occurs because of a difference in the names of the table created by Entity Framework and the table referenced by the view.

The table created by Entity Framework follows the convention of pluralizing the name of the entity. Therefore, in this case, a table named "Votes" is generated. However, the view expects a table named "Vote." The main reason for this is the MySQL .net connector's support of EF. Removing pluralization and making other adjustments should resolve the issue.

Firstly, the "ApplicationStart" method contains a minor error:

// Database.SetInitializer(new DropCreateDatabaseAlways<myDB>());
Database.SetInitializer(new myDBInitializer());

In addition, disabling the base implementation of "OnModelCreating" is required:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // modelBuilder.Entity<Vote>().ToTable("Votes")
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

It has also been suggested that the MySQL .net Connector prevents EF from producing databases, necessitating the creation of a blank DB beforehand. This limitation does not apply to connector versions 6.4.4 in the current scenario. Creating the database is preferable if the user has the required permissions.

These adjustments should resolve the discrepancy between the plural table name created by Entity Framework and the singular table name expected by the view.

The above is the detailed content of Why Does Entity Framework Create a Plural Table While My View Expects a Singular Table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn