Daniel Rapaport Daniel Rapaport - 1 month ago 11
C# Question

EF db first Edmx database types nvarchar vs varchar

I have table mapped to EF that originally had a varcahr column , I changed it on SQL server to nvarchar and before changing it on EF i tested the app and everything seemed ok.
now not sure I understand why it worked and why I am not required to change Edmx from varchar to nvarchar.

<Property Name="CustomerFirstName" Type="varchar" MaxLength="100" Nullable="false" />

and not

<Property Name="CustomerFirstName" Type="nvarchar" MaxLength="100" Nullable="false" />

Answer Source

SQL Server implicitly converts VARCHAR into NVARCHAR (and vice versa). IOW, if you pass in a VARCHAR where it's expecting NVARCHAR, it will automatically make the conversion internally without telling you (or at least without telling you in an obvious way).

I would still recommend that you make your data types match between EF and the database so that you don't perform potentially costly implicit conversions, or run into errors when attempting to convert incompatible data/types.