Using dynamic Table attribute with entity framework

Also wanted to use some dynamic into the Entity Framework Table attribute like this:

[Table(ConfigurationManager.AppSettings["Prefix"] + "TABLENAME")]
public class Tablename: BaseEntity, IEquatable<Tablename>
{
    ....
}

using the following app-setting from web.config

<appSettings>
<add key="Prefix" value="TEMP_" />
</appSettings>

This will end up with the following error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Instead create the following class:

using System.ComponentModel.DataAnnotations;
using System.Configuration;

namespace Domain.Extensions
{
     public class CustomTableAttribute : TableAttribute
     {
         public CustomTableAttribute(string name)
             : base(ConfigurationManager.AppSettings["Prefix"] + name)
         {
         }
     }
 }

and now you can use:

[CustomTable("TABLENAME")]
public class Tablename: BaseEntity, IEquatable<Tablename>
{
    ....
}

Good luck,

Martijn

Geef een antwoord

Het e-mailadres wordt niet gepubliceerd.

*
*
*

Back To Top