views:

70

answers:

3

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.

+1  A: 

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?

pault
@Pault, Thank you for your time. Yes, I am trying to target a particular table name ( which is not plural in this case). I am using very similar code like you suggested and getting an error. See my updated question.
ydobonmai
A: 

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?

pault
Paul, i have an existing database and I am designing an object model around it. So, first I created my POCOs and thought of using the code-first and hit this wall. Is code-first not the way to go in this case? If yes, why i m getting this error? If no, wht is the best way in this case?
ydobonmai
Well, I'm still learning this stuff myself, so I'm not entirely sure the details of doing it with an existing database. That said, Scott Gu has a blog post about the very thing: http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
pault
+2  A: 

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.

pault
Right track!!! Database.SetInitializer<AddressBook>(null) is the way to go. I needed to include this as the first line in my program. Thanks a tonne! :-) I read that article, however, I didn't look at the comments. Lesson learnt :- Reading comments in a great blog post is a useful thing.
ydobonmai
I wonder how Scot could run the sample he has in the sample he has on http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx where he works with an existing database and map to a diffrent table (tblDinners instead of Dinners).
ydobonmai
@Pault, I described this problem and the solution here :- http://ashishrocks.spaces.live.com/blog/cns!BED6CE34F24CB429!540.entry
ydobonmai
Right on, I'm glad it worked!
pault

related questions