Entity Framework – CTP4 – Code First – How to turn off the automatic pluralization

code-firstentity-framework-4

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 any idea on how to turn off the pluralization support?

This post has got some details on pluralization support. But still does not give me an answer.

I see the following text in this post. First of all, I dont know which physical .tt file I need to make this change. Also, I want to have this feature turned off only for one app and not for all.

The code generator in T4 Toolbox has
the pluralization turned on by default
in Visual Studio 2010. If you need to
generate the DAL without
pluralization, perhaps for
compatibility reasons, you can turn
this option off by adding the
following line to the .tt file before
generator.Run() method is called.

C#
generator.Pluralize = false;

VB
generator.Pluralize = False

UPDATE

Following is the code I use and I get an error given below:-

Contact

 public class Contact
 {
 public int ContactID { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Title { get; set; }
 public DateTime AddDate { get; set; }
 public DateTime ModifiedDate { get; set; }
 }

Context:-

 public class AddressBook : DbContext
 {
 public DbSet<Contact> Contact { get; set; }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
  modelBuilder.Entity<Contact>().MapSingleType().ToTable("dbo.Contact");
 }

}

The main program:-

using (var context = new AddressBook())
  {
   var contact = new Contact
   {
   ContactID = 10000,
   FirstName = "Brian",
   LastName = "Lara",
   ModifiedDate = DateTime.Now,
   AddDate = DateTime.Now,
   Title = "Mr."

   };
   context.Contact.Add(contact);
   int result = context.SaveChanges();
   Console.WriteLine("Result :- " + result.ToString());

  }

And I get the following error on "context.Contact.Add(contact);":-

System.InvalidOperationException: The
model backing the 'AddressBook'
context has changed since the database
was created. Either manually
delete/update the database, or call
Database.SetInitializer with an
IDatabaseInitializer instance. For
example, the
RecreateDatabaseIfModelChanges
strategy will automatically delete and
recreate the database, and optionally
seed it with new data. at
System.Data.Entity.Infrastructure.CreateDatabaseOnlyIfNotExists1.InitializeDatabase(TContext context) at System.Data.Entity.Infrastructure.Database.Initialize() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.EfInternalQuery1.Initialize()
at
System.Data.Entity.DbSet1.ActOnSet(Action action,EntityState newState, TEntity entity) at System.Data.Entity.DbSet1.Add(TEntity
entity) at
CodeFirst.Client.Program.Main(String[]
args) in E:\Ashish\Research\VS
Solutions\EntityFramework\CodeFirstApproach_EF_CTP4\CodeFirst.Client\Program.cs:line 35

I am sure I am making a stupid mistake somewhere, just unable to figure out. Could somebody please provide some directions?

ANSWER
With Pault's help I described this problem and the solution here.

Best Answer

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.