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.CreateDatabaseOnlyIfNotExists
1.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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With