开发者

Incoherent error: invalid object name

I renamed a table from my database. So I also renamed my entity (from Favori to FavorisAffaire). Compiled successfully. But now, when I run my application, I get the error below:

Invalid object name 'dbo.FavorisAffaires'

I didn't have this FavorisAffaires in my solution!

I have FavorisAffaire (withount ending s). I suspect something is wrong in the background of my solution... I already try a clean followed by a rebuild but the problem is still there.

An开发者_如何学Pythony ideas?


That is how EF behaves by default. It uses pluralization of names. It can be turned off or you can force single entity to map to proper table but that all depends on the version of EF you are using.

Edit:

oops I forgot to mention that I used Entity Framework Code First.

Yes you forgot to mention very important part of your question.

There are several ways to do that.

Data annotation:

[Table("FavorisAffaire")]
public class FavorisAffaire
{ ... }

Or fluent mapping:

modelBuilder.Entity<FavorisAffaire>()
            .ToTable("FavorisAffaire");

Or removing pluralization convention (this has global scope so it can break mappings of other existing tables)

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

Once you remove convention you should ensure that name of DbSet is singular:

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

Or simply rename your table because tables should be plural.


In addition to Ladislav Mrnka's answer, I assume that you haven't recreated your database, after you made these changes. So "Code First" generated a table-name for your new entity, that didn't exist before (to be precise, it was probably named "Favoris"). If you now connect to a database that already has the old schema, and you did not order EF to recreate the database, it will assume, that the table it is looking for (FavoriAffairs) is present (which it isn't).

If you already have data in your "Favoris" table, I suggest you rename it to "FavoriAffairs", otherwise let Code First recreate the database. This is done be setting a specific database-initializer, explained here: http://davidhayden.com/blog/dave/archive/2011/04/27/DatabaseInitializersEFCodeFirstDatabaseSetInitializer.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜