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.EfInternalQuery
1.Initialize()
at
System.Data.Entity.DbSet1.ActOnSet(Action
action,EntityState newState, TEntity
entity) at
System.Data.Entity.DbSet
1.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.
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(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
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?
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?
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
}