locked
is it possible to change table name with complex in codefirst RRS feed

  • Question

  • hi;

    I am developing a application that will integrate with another application,other application table name like Customer_2020 and it will change the next year as Customer_2021 ,I defined the Entity table [Table("Customer_2020")] but I have to change next year ,in this case I want to ask a question , thus ;

    var key="2020" from web.config or app.config

    [Table("Customer_"+key+"")]

    is it possible or not .

    thanks in advance.

    Friday, April 24, 2020 10:51 AM

All replies

  • Hi Aly14,
    You can use data annotations or fluent api to change the table name.
    Data Annotations:

    //Changing database table name to Metadata
    [Table("Customer_2021")]
    public class Metadata 
    {
      [Required, Key]
      public int MetadataId { get; set; }
    
      [Required, ScaffoldColumn(false)]
      public int DocumentId { get; set; }
    
      [Required, StringLength(250), DataType(DataType.Text)]
      public string Title { get; set; 
    }

    Fluent Api:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      //Changing Database table name to Metadata
      modelBuilder.Entity<Metadata>()
          .ToTable("Customer_2021");
    }

    Here is a related thread you can refer to.
    Hope it could be helpful.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 27, 2020 7:56 AM
  • I think you misunderstand to me , I asked and you answered is different thing each other.
    Monday, April 27, 2020 9:06 AM
  • Hi Aly14,
    Sorry for my misunderstanding, do you want to get the value of key from app.config?
    If so, you can use System.Configuration to get the value in the app.config.
    First, you can add a reference to System.Configuration in your project.
    Here is a simple code example.
    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="year" value="2010" />
      </appSettings>
    </configuration>

    Access the value:

    string key = ConfigurationManager.AppSettings["year"];

    Then change a table name by referring to the link above.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 27, 2020 10:04 AM
  • I am so sorry , but ,you got it wrong again ,I wanted to say that ,

    my entity table name=[Table("Customers_2021")]

    I wanto to change this,

    var preFix="2021"

    [Table("Customers_"+preFix+"")]

    I hope its enough .


    Monday, April 27, 2020 10:13 AM
  • Hi Aly14,
    As mentioned above, you can use the Fluent Configuration API:

    public class YourDbContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var preFix="2021"
            modelBuilder.Entity<Product>().ToTable("Customers_"+preFix+"");
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 28, 2020 9:49 AM