Search code examples
entity-framework-4

How to specify name for entity configuration


I'm using code-first in entity-framework 4, Entity Framework Feature CTP 3

Is it possible to specify an entity configuration's name? In effect changing the table name from 'xxxSet' to a name of my choice?


Solution

  • Found the documentation. You want to do something like:

    public class BloggingModel : ObjectContext
    {
        public BloggingModel(EntityConnection connection)
            : base(connection)
        {
            DefaultContainerName = "BloggingModel";
        }
    
        public IObjectSet<User> Users   // ObjectSet name -- you can call it whatever you want
        {
            get { return base.CreateObjectSet<User>(); }
        }
    }
    
    class UserConfiguration : EntityConfiguration<User>
    {
        public UserConfiguration() 
        {
            Property(u => u.Password).HasMaxLength(15).IsRequired();
    
            Relationship(u => u.AuthoredPosts).FromProperty(p => p.Author);
            Relationship(u => u.PostedPosts).FromProperty(p => p.Poster);
    
            MapHierarchy( 
                u => EntityMap.Row( 
                    EntityMap.Column(u.ID, "uid"),
                    EntityMap.Column(u.Password)
                )
            ).ToTable("Users");  // DB table name -- again, anything you like
        }
    }
    

    Again, see the linked post for full info.