Jason Jacobs
Jason Jacobs

Reputation: 13

EF 6, .NET 4.8 : resulting query doesn't seem to use StringLength

I've seen similar questions and have tried applying the answers, but to no avail, so I wanted to ask my question with more context of what I've tried.

My property looks like this:

[Column("colnm")], TypeName ="varchar")]
[StringLength(4)]
public string ColumnName { get; set; }

The resulting select query becomes this:

@p__linq__0 varchar(8000)

I need it to be this:

@p__linq__0 varchar(4)

When it's 8000, it does a table scan and the query is not performant at all. The column in the database is defined as varchar(4), so I'm trying to tell EF to use varchar(4).

To be clear, we had the tables set up before we needed this entity, and this is not a migration. I'm just trying to get EF to handle a select query on my entity.

I've tried only using StringLength, changing it to MaxLength, putting it inline with TypeName("varchar(4)"), and all of the above in FluentAPI as well in my context. It always stays at varchar(8000).

I know that my entity impacts the query, because when I change varchar to nvarchar, I see the 8000 drop to 4000. But it never gets down to 4.

I can't find anywhere that's explicitly overriding my StringLength either.

Even ChatGPT provided suggestions that made sense, but none of them worked. :)

Ideas?

Upvotes: 1

Views: 126

Answers (0)

Related Questions