Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid object name 'Entity' using base type (Inheritance) #11779

Closed
thiagomajesk opened this issue Apr 23, 2018 · 1 comment
Closed

Invalid object name 'Entity' using base type (Inheritance) #11779

thiagomajesk opened this issue Apr 23, 2018 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@thiagomajesk
Copy link

thiagomajesk commented Apr 23, 2018

I'm trying to use a common base type for entities using this workaround: #8728.

[NotMapped]
public abstract class Entity
{
    public int Id { get; set; }

    public DateTime CreatedOn { get; set; }

    public DateTime UpdatedOn { get; set; }

    public DateTime? DeletedOn { get; set; }
}

public class Company : Entity
{
    /* [...] */
}

But I'm having a problem trying to access the generic db set like this:

// where TEntity : Entity, new()
context.Set<TEntity>();

// or...
context.Set<Company>();

The error is Invalid object name 'Entity'. and this is the stack trace:

   em System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   em System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   em System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   em System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   em System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   em System.Data.SqlClient.SqlDataReader.get_MetaData()
   em System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
   em System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   em System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   em System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   em System.Data.Common.DbCommand.ExecuteReader()
   em Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   em Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   em Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
   em Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   em Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   em Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
   em Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   em Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
   em Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   em System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

Does anyone knows exactly why this is going on?

PS.: abstract wasn't enough for EF Core to generate the migrations for the derived classes. I had to use [NotMapped] because I was getting migrations only for the base class (Entity).

@thiagomajesk
Copy link
Author

I've discovered that this line on my OnModelCreating was the cause:
modelBuilder.Entity<Entity>().HasQueryFilter(e => e.DeletedOn == null);

From #8728 (comment):

This implies that there cannot be any references from entity types to the CompanyBase class--they must all reference one of the derived classes

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label May 3, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

2 participants