Entity Framework 6 and const vs static readonly

Note: I haven’t tested this on Entity Framework Core, but I imagine the behavior is the same

Let’s say you have the following class:

public static class BlargTypes {
     public static readonly string Foo = "Bar";

and then you have the following LINQ query:

var result = dbContext.Blargs.Where(x => x.Blarg == BlargTypes.Foo);

then Entity Framework will generate a SQL query that looks like this:

SELECT [Id],[Blarg] FROM [Blargs] WHERE [Blarg] = @Blarg

passing in “Bar” as the parameter value for @Blarg

Now, what if the value never changes? There’s really no need for a parameterized value for @Blarg. How do you manage that?

public static class BlargTypes {
    public const string Foo = "Bar";

Entity Framework will generate the following query:

SELECT [Id],[Blarg] FROM [Blargs] WHERE [Blarg] = 'Foo'

In summary, if you want a parameterized query, use static readonly, otherwise use const.