EF Core generates statement with invalid table name

0

I'm trying to get some values with Include and ThenInclude but EF Core generates a statement with an invalid tablename.

This is my DataContext:

public class BaseContext : BaseDbContext 
{
    protected BaseWebsiteContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
    {
    }

    public DbSet<Image> Images { get; set; }
    public DbSet<Tag> Tags { get; set; }
    public DbSet<ImageTags> ImageTags { get; set; }
}

These are my three datamodels:

public class Image : Entity.Entity
{
    public string Title { get; set; }
    public string Filepath { get; set; }
    public DateTime CreateDate { get; set; }

    public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
}

public class Tag : Entity.Entity
{
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
}

public class ImageTags
{
    public int ImageId { get; set; }
    public Image Image { get; set; }
    public int TagId { get; set; }
    public Tag Tag { get; set; }
}

the base-class Entity contains the property of primary-key Id (int) In my OnModelCreating, I added to define the composite PK.

modelBuilder.Entity<ImageTags>().HasKey(x => new {x.ImageId, x.TagId});

So in my repository, this is my query:

return await _dataContext.Images
            .Include(x => x.ImageTags)
                .ThenInclude(x => x.Tag)
            .Where(w => w.ImageTags.Any(x => x.TagId == tagId))
            .ToListAsync();

And this is what EF generates:

SELECT [x.ImageTags].[ImageId], [x.ImageTags].[TagId], [i.Tag].[Id], [i.Tag].[Description], [i.Tag].[Name]
FROM [ImageTags] AS [x.ImageTags]
INNER JOIN [Tags] AS [i.Tag] ON [x.ImageTags].[TagId] = [i.Tag].[Id]
INNER JOIN (
    SELECT [x1].[Id]
    FROM [Images] AS [x1]
    WHERE EXISTS (
        SELECT 1
        FROM [ImageTags] AS [x2]
        WHERE ([x2].[TagId] = @__tagId_0) AND ([x1].[Id] = [x2].[ImageId]))
) AS [t] ON [x.ImageTags].[ImageId] = [t].[Id]
ORDER BY [t].[Id]

that is wrong, because my table is called Tag, not Tags, so of course, it throws an SqlException System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Tags'.

Any idea what I did wrong?

Edit: I don't want to change the tables name. If I add a migration, EF generates my migration/sql-table. How can it be that there appears an exception?

c#
entity-framework-core
sqlexception
asked on Stack Overflow Jun 18, 2019 by Matthias Burger • edited Jun 18, 2019 by Matthias Burger

1 Answer

5

Your DbContext DbSet for Tag is public DbSet<Tag> Tags { get; set; }. In EF/EF Core DbContext's DbSet properties maps to database table. Hence it is searching table name Tags for Tag as your DbSet for Tag is Tags.

You can solve it in any of the following ways:

  1. public DbSet<Tag> Tag { get; set; }
  2. Renaming Tag table to Tags
  3. modelBuilder.Entity<Tag>().ToTable("Tag");
answered on Stack Overflow Jun 18, 2019 by TanvirArjel

User contributions licensed under CC BY-SA 3.0