0

I wonder if this is even possible, and I can't seem to find an answer. I am using Entity Framework / C# for a database application. I want to ensure that my front-end entities automatically match their counterparts in SQL for attributes like "StringLength". So, inside SQL, in my tables, if I specify, for example, "nvarchar(50)", I want to make sure that the user doesn't type more than 50 characters. I don't want to have to code this each time, so I'm looking for a way to have the C# application read the StringLength property from SQL and then apply logic in the front-end. I can't seem to find a way of getting this information by entity type/DbContext from the SQL database itself.

I thought I'd struck gold with this thread here: How to get the maximum length of a string from an EDMX model in code? but this seems to read from the entity schema defined in the C# application, not SQL.

I have attached a .png of the info I am trying to get from SQL for clarification. Any advice would be appreciated:enter image description here

Davy C
  • 639
  • 5
  • 16

1 Answers1

0

use this query to return Maximum length of column Description in table PaymentType

SELECT
    CHARACTER_MAXIMUM_LENGTH
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_NAME = 'PaymentType' and COLUMN_NAME = 'Description' 
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
  • Great! Many thanks. I didn't think of pulling it back through a SQL query. That should do the trick. I will test that out. – Davy C Nov 16 '20 at 14:46