4

I'm working with Entity Framework Core, v6.2. I'm getting an error

SqlException: Invalid object name 'Cdef.CellDefinition'

when I try to access the DbSet directly, but using the same DbContext object, I can query the object directly using the FromSql command.

I've seen other answers saying to modify the conventions to remove PluralizingTableNameConvention, however since I'm doing a EntityFrameworkCore.DbContext that ModelBuilder does not have that option, and I don't see any evidence it is try to access a Pluralized name.

My entity is setup like:

[Table("Cdef.CellDefinition")]
public partial class CellDefinition
{
    [Key]
    public int Id { get; set; }
}

And my DbContext is like:

public class CDefContext : Microsoft.EntityFrameworkCore.DbContext
{
    public virtual Microsoft.EntityFrameworkCore.DbSet<CellDefinition> CellDefinition { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

When I try to access the object as an entity directly, I get an error:

Invalid Object Name

but if I issue the SQL with the same object name it works properly.

// This fails with Invalid Object Name
return cDefContext.CellDefinition.ToList();

// This succeeds
return cDefContext.CellDefinition.FromSql("select * from CDef.CellDefinition").ToList()
1
  • Just a side note: the EF Core is currently at version 2.1 (which 2.2 in the works) - version 6.2 belongs to the "legacy" (non .NET Core) version of EF ..... Commented Oct 25, 2018 at 4:12

1 Answer 1

9

I found the solution. You can't put the schema in the table name.

//This Does NOT work
[Table("Cdef.CellDefinition")]
public partial class CellDefinition{}


//But this DOES work
[Table("CellDefinition",Schema = "Cdef")]
public partial class CellDefinition{}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.