LINQ puts unnecessary trailing spaces on lines

I have a very nasty problem with LINQ and MS SQL Server.

Im currently creating some software in C # WPF and is using LINQ to generate classes from the data in the database.

However, when I update the nvarchar or varchar field in the database from my program, LINQ adds trailing spaces to the string!

I have a field in a table defined as follows:

ProductName = NVARCHAR(10)

So, if I do this:

Product.Name = "Bike";
Product.Name = Product.Name.Trim();
repository.Add(Product);   // Runs an .InsertOnSubmit
repository.Save();         // Runs a .SubmitChanges

then the resulting data in the database: "Bike [SPACE] [SPACE] [SPACE] [SPACE] [SPACE] [SPACE]", where [SPACE] is just a space (cannot write several spaces in this text here).

Why is he doing this, and how do I get him to stop adding these annoying trailing spaces?

Thanks in advance

+3
3

LINQ NVarchar :

[Column(Storage="_Text", DbType="NChar(20) NOT NULL", CanBeNull=false)]
public string Text
{
get
{
    return this._Text;
}
set
{
    if ((this._Text != value))
    {
        this.OnTextChanging(value);
        this.SendPropertyChanging();
        this._Text = value;
        this.SendPropertyChanged("Text");
        this.OnTextChanged();
    }
}
}

:

[Column(Storage="_Text", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]

- , SQL LINQ.

+2

, ... - linq, - . VARCHAR.

+5

Nchar .

: Nchar Nvarchar

db . .

, ...

char, nchar, varchar nvarchar SQL Server?

, mssql trim.

0

Source: https://habr.com/ru/post/1725258/


All Articles