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

好久不见. 提交于 2019-11-30 09:49:23

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>();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!