0

I am trying to set the TableAttribute for my view like so:

[Table(LSODatabase.databaseString)]

However the following error is thrown:

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

A workaround I am trying to avoid is making another view and manually setting the strings, however I have a lot of views and would like to avoid this as much as possible.

TheShield
  • 297
  • 1
  • 5
  • 18

2 Answers2

0

The error is somewhat self explaining. The arguments you put into your attribute constructors have to be a constant expression, typeof expression or array creation expression. The databaseString field of your LSODatabase isn't constant unless you add the const modifier. Constant means that you can say at compile time what the value of the property will be and that it won't be able to change at runtime.

nikstffrs
  • 344
  • 1
  • 7
0

As the error says, you must use either a constant, a typeof expression or an array creation.

One possible solution would be:

public class LSODatabase
{
    public const string databaseString = "myDatabase";
}

Now you can use that as an argument for your attribute.

germi
  • 4,628
  • 1
  • 21
  • 38
  • This won't do as I need to be able to change the string. – TheShield Oct 18 '19 at 20:02
  • Yes, that is not possible - as the error states. And frankly, it doesn't really make sense to want to be able to change the table name at runtime. – germi Oct 18 '19 at 20:04