C# Parameterized Queries for Oracle - Serious & Dangerous Bug!

Entity Framework creates a plural table name, but the view expects a singular table name?

So I gave up on trying to do it the way I felt it should be done and removed pluralization all together. I don't really know for certain, but I assume the problem has to do with the mysql .net connector's support of EF. Here is what I did.

First, there was a bug in my ApplicationStart method:

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

Second, I stopped calling the OnModelCreating base implementation which is not listed in the original code since I only implemented it as per jgauffin's suggestion:

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

Third, I read in some posts that the MySQL .net Connector doesn't let EF actually CREATE a database, so I had initially created the blank DB. This seems to no longer be the case with connector 6.4.4+, and as long as your connection string's user has the ability to create new databases, it works better if one is not existing initially.

Once, I did all of the above, it seemed to work. So now I can at least move forward. Hopefully we can figure out the cause of the plural / singular discrepancy in the future.

Thanks to everyone for their time and effort.

Entity Framework 7 pluralize table names with code first approach

you can do this in the OnModelCreating overload like -

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
}
}

you can also do this by using "data annotations"

    [Table("blogs")]
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}

or Fluent Api

class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable("blogs");
}
}

for more details have a look at - Documentation for EF7

ADO.NET Entities 3.5 - Plural naming broken?

I solved the problem by using the third-party add-in Huagati DBML Tools: http://www.huagati.com/dbmltools/ which performs pluralisation of entity names.

EF6 Code First Pluralizing Tables Invalid Object Name

Figured out my own mistake. I have two Context classes, one inheriting from the other for the repository, and I had the code below in the base context, when I moved it to the derived it now works. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

Entity Framework (4.3) looking for singular name instead of plural (when entity name ends with s)

This is probably happening because even though the intention was to use the Database First flow, in actual fact the application is using Code First to do the mapping. Let me explain a bit more because this can be confusing. :-)

When using Database First with the EF Designer and the DbContext templates in Visual Studio three very important things happen. First, the new Entity Data Model wizard adds a connection string to your app containing details of the Database First model (i.e. the EDMX) so that when the application is run it can find this model. The connection string will look something like this:

<connectionStrings>
<add name="MyEntities"
connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=MyEntities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>

Second, the generated context class makes a call to the base DbContext constructor specifying the name of this connection string:

public MyEntities()
: base("name=MyEntities")
{
}

This tells DbContext to find and use the "MyEntities" connection string in the config. Using "name=" means that DbContext will throw if it doesn't find the connection string--it won't just go ahead and create a connection by convention.

If you want to use Database First, then you must use a connection string like the one that is generated. Specifically, it must contain the model data (the csdl, msl, ssdl from the EDMX) and you must make sure that DbContext finds it. Be very careful when changing the call to the base constructor.

The third thing that happens is that OnModelCreating is overridden in the generated context and made to throw:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}

This is done because OnModelCreating is only ever called when using Code First. This is because OnModelCreating is all about creating the model, but when you are using Database First the model already exists--there is nothing to create at runtime. So if OnModelCreating is called then it is probably because you started using Code First without meaning to, usually because of a change to the connection string or the call to the base constructor.

Now, it might be that you want to use Code First to map to an existing database. This is a great pattern and fully supported (see http://blogs.msdn.com/b/adonet/archive/2011/03/07/when-is-code-first-not-code-first.aspx) but you will need to make sure mappings are setup appropriately for this to work. If the mappings are not setup correctly then you will get exceptions like the one in this question.



Related Topics



Leave a reply



Submit