My entity name is \"Contact\" and my table name is \"Contact\". However, the default pluralization support is making EF4 to look for a table named \"Contacts\". Anybody has
Are you simply trying to target a particular table name?
If so, this may be the answer you are looking for:
public class FooDataContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
}
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity<Contact>().MapSingleType().ToTable("Contact");
}
}
Does this help or did I completely miss the point?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
I'm adding yet a third answer as my understanding of the question changes... is that bad of me? ;)
Like I said in my comment, I'm still learning and I haven't attempted this with an existing database yet. That said, hopefully one of these will help:
The post I mentioned by Scott Guthrie (http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx) has a comment by a certain Jeff that says this can help (I recommend reading the full comment as he explains in more detail):
Database.SetInitializer<AddressBook>(null); //AddressBook being the context
I've also happened across a comment by Rowan Miller underneath this post (http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4announcement.aspx?PageIndex=2) in the recent past. He suggests this may be an option:
public class ProductContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.IncludeMetadataInDatabase = false;
}
...
}
Hopefully one of these gets you on the right track.
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
}
Out of the box, the code-first stuff will not modify or delete an existing database. I'm guessing you have an existing database that you either created manually or the framework created by running your program earlier, and then you changed your Contact class. The fast way to fix this error one time would be to manually delete your database, or update it to match your model.
However, the real power of the code-first feature set is to allow rapid domain creation and refactoring without having to worry about creating and maintaining database scripts early in development. To do this, I personally have found it useful to allow the framework to delete my database for me every time I make a change to the model. Of course, that also means that I'd like to seed the database with test data.
Here is how you can accomplish this:
public class MyCustomDataContextInitializer : RecreateDatabaseIfModelChanges<MyCustomDataContext> //In your case AddressBook
{
protected override void Seed(MyCustomDataContext context)
{
var contact = new Contact
{
ContactID = 10000,
FirstName = "Brian",
LastName = "Lara",
ModifiedDate = DateTime.Now,
AddDate = DateTime.Now,
Title = "Mr."
};
context.Contact.Add(contact);
context.SaveChanges();
}
}
Then you need to register this initializer (say in Application_Start(), if it's a web application):
Database.SetInitializer(new MyCustomDataContextInitializer());
Note that you'll need:
using System.Data.Entity.Infrastructure;
Does this help?