Entity Framework Generates SQL Queries

I have a 2 EF object defined as:

public class Event
{               
    public DateTime Created { get; set; }
    public string Id { get; set; }
    public bool Public { get; set; }
    public EventType Type { get; set; }

    public virtual ICollection<Note> Notes { get; set; }              
}

public class Note
{
    public string EventId { get; set; }      // Link to parent event
    public int Id { get; set; }
    public string Text { get; set; }

    public virtual Event Event { get; set; }
}

When accessing the collection Notesin an EventSQL query, it is built in the form:

exec sp_executesql N'SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[EventId] AS [EventId], 
    [Extent1].[Text] AS [Text]
    FROM [dbo].[Notes] AS [Extent1]
    WHERE [Extent1].[EventId] = @EntityKeyValue1',
    N'@EntityKeyValue1 nvarchar(128)',
    @EntityKeyValue1=N'N5427961'

This query, given the data size used and indexing, exceeds 3,000 views. This seemed a bit big, given that the tables have sufficient indexing. The query analyzer had no suggestions to speed up the query.

Then I found that if I changed the data type used in the SQL binding from nvarchar(128)to varchar(33), which exactly matches the column type EventIdin the database table, now only 8 queries are required.

Is this just the case when I need to use DataAnnotations to tell EF what data types in SQL are these fields? or is something else happening here?

+4
1

:

modelBuilder.Entity<Event>().Property(x => x.Id).IsUnicode(false).HasMaxLength(33);

, , nvarchar.

+1

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


All Articles