There is an EF6 Code-First. When the Property entity is saved, spaces are removed at the end and / or beginning of the value of the Name property. The problem is that I need to keep these spaces, because This behavior does not allow to save entities differing in their spelling. Anticipating criticism that the preservation of such data is a mistake, I will explain that the program is designed specifically for analyzing and collecting such data.
How can I change the behavior of EntityFramework and allow whitespace to be stored at the end and / or beginning of the property value?
public partial class MyContext : DbContext { public MyContext() : base("name=MyContext") { } // ... protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Property>() .Property(e => e.Group) .IsFixedLength(); modelBuilder.Entity<Property>() .Property(e => e.Name) .IsFixedLength(); } } public partial class Property { public SEProperty() {} public int Id { get; set; } [Required] public string Group { get; set; } [StringLength(255)] public string Name { get; set; } } The table creation script looks like this:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Property]( [Id] [int] IDENTITY(1,1) NOT NULL, [Group] [nvarchar](50) NOT NULL, [Name] [nvarchar](255) NULL, CONSTRAINT [PK_dbo.Property] PRIMARY KEY CLUSTERED ( [Id] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
IsFixedLength(), then the columns are created likenvarchar. Spaces are preserved and do not disappear anywhere. What is your project type - ASP.NET? I can assume that the data from the database are displayed in html. Accordingly, whitespace characters are cropped by the browser. - Alexander Petrovselect * from [Property] where [Name] = 'abc ', trailing spaces are ignored and the entry with'abc'returned. - XelaNimed