none
Is it possible to add a Common Prefix to Table Names in EF RRS feed

  • Question

  • I have a database that will be shared by several applications.  Typically we would have AppName_TableName utilizing other methods of connecting.  

    Is there something that I can override that will when it goes to create the table.  I know that you can override at the class level of the entity model, but I foresee issues as other people not place add that attribute to the class.

    Thank you, 


    Sunday, January 10, 2016 3:47 PM

Answers

  • You can use either the class annotations or the fluent API to control the table names.  Or you can add a custom convention to your DbContext.  Something like this

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                var pluralizationService = (IPluralizationService)DbConfiguration.DependencyResolver.GetService(typeof(IPluralizationService), null);
                     
                modelBuilder.Types().Configure(c => c.ToTable("SOMEAPP_" +pluralizationService.Pluralize(c.ClrType.Name).ToUpper()));
            }

    David


    David http://blogs.msdn.com/b/dbrowne/


    Sunday, January 10, 2016 4:53 PM

All replies

  • You could of course use database first and then they get the name of the tables the dba gives them.

    Personally, I far prefer database first.

    The least of your problems with code first is some naming convention.

    .

    If you need to stick with code first then you coul use a type level convention.

    For example,  pluralisation:

    private string GetTableName(Type type) 
    { 
        var pluralizationService = DbConfiguration.DependencyResolver.GetService<IPluralizationService>(); 
     
        var result = pluralizationService.Pluralize(type.Name); 
     
        result = Regex.Replace(result, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]); 
     
        return result.ToLower(); 
    }


    I don't know where you're getting your application from but you can do something like:

    private string GetTableName(Type type) 
    { 
        return "AppName_" + type.Name; 
    }

    See

    https://msdn.microsoft.com/en-us/data/jj819164.aspx?f=255&MSPPError=-2147217396


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Sunday, January 10, 2016 4:40 PM
  • You can use either the class annotations or the fluent API to control the table names.  Or you can add a custom convention to your DbContext.  Something like this

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                var pluralizationService = (IPluralizationService)DbConfiguration.DependencyResolver.GetService(typeof(IPluralizationService), null);
                     
                modelBuilder.Types().Configure(c => c.ToTable("SOMEAPP_" +pluralizationService.Pluralize(c.ClrType.Name).ToUpper()));
            }

    David


    David http://blogs.msdn.com/b/dbrowne/


    Sunday, January 10, 2016 4:53 PM