4

I have an 2 EF entity 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 Notes collection in Event, a SQL query is constructed 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 and indexing used required in excess of 3000 reads. This seemed a bit much given the tables have sufficient indexing. Query analyser had no suggestions to speed the query up.

I then discovered if I changed the datatype being used in the SQL bind from nvarchar(128) to varchar(33), which matches exactly the column type of EventId in the DB table, the query now only needed 8 reads.

Is this simply a case of me needing to use DataAnnotations to tell EF what the data types in SQL these fields are? or is something else happening here?

1 Answer 1

1

I found the way to most easily get the desired effect was:

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

The other suggested method using annotation did not work and resulted in nvarchar still being used.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.