C# Entity Framework Invalid Object name 'sys.default_constraints'

Asked

Viewed 717 times

1

In my C# MVC 5 project with Entity Framework together with an SQL Server, among a series of classes, I have a class called ctmdMandados.Cs:

public class ctmdMandados
{
    //cadastro dos mandados recebidos e seus status
    public int Id { get; set; }

    //outras propriedades

    public virtual int TipoDocID { get; set; }
    public virtual ctmdTipoDoc TipoDoc { get; set; }


}

It happens that before the variable Tipodoc was a local Enum with fixed values, duly migrated in the Entity Framework. However, during the modeling, I decided to create a new class for Tipodoc, with a new table, primary key and everything. Prior to this change, the database was functioning normally, with no pending migration.

public class ctmdTipoDoc
{
    public int Id { get; set; }
    [StringLength(12)]
    public string TipoDoc { get; set; }

    public virtual IList<ctmdMandados> Mandados { get; set; }

}

And then Mapeei in Dbcontext:

public class AplicacoesContexto: DbContext
{
//outros mapeamentos
    public DbSet<ctmdTipoDoc> ctmdTipo { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
//outros modelbuilders
        var ctmdTipoBuilder = modelBuilder.Entity<ctmdTipoDoc>();
        ctmdTipoBuilder.ToTable("ctmdTipo");

        modelBuilder.Entity<ctmdTipoDoc>()
            .HasMany(ctmdTipo => ctmdTipo.Mandados)
            .WithRequired(ctmdMandados => ctmdMandados.TipoDoc);
}

After, I ran the code to add a new migration, and the Entity Framework: created the following (pasted only the Up):

public partial class Atualiza : DbMigration
{
    public override void Up()
    {

        CreateTable(
            "dbo.ctmdTipo",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    TipoDoc = c.String(maxLength: 12),
                })
            .PrimaryKey(t => t.Id);

        AddColumn("dbo.ctmdMandados", "TipoDocID", c => c.Int(nullable: false));
        CreateIndex("dbo.ctmdMandados", "TipoDocID");
        AddForeignKey("dbo.ctmdMandados", "TipoDocID", "dbo.ctmdTipo", "Id", cascadeDelete: true);
        DropColumn("dbo.ctmdMandados", "Tipo");
    }}

When trying to run update-database, the Package Manager Console generates the error Invalid Object name 'sys.default.constraints' (last line):

Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201602252013494_Atualiza].
Applying explicit migration: 201602252013494_Atualiza.
System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'sys.default_constraints'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:b0d48a01-5a3f-469d-bd07-70a280743ba9
Invalid object name 'sys.default_constraints'.

I looked it up online, but I couldn’t find anything definitive. I think it has to do with the standards next to SQL Server, but I have no idea how to fix.

Thanks again for your attention!

  • What version of SQL Server?

  • SQL Server 2010 or 2012, I believe. The server is not in my department.

  • Bank user has some access limitation?

  • Not to the selected database. I cannot log into the database of other departments on the same server

  • You can run with your user the following? select * from sys.default_constraints

  • Dear, I confess that I already left work at the time, I will take the test tomorrow :)

  • try to remove your dbo.

  • @Romaniomorrisonmendez When trying to perform the query in SQL server returns the same error "Invalid Object name 'SYS.DEFAULT_CONSTRAINTS'."... how strange

  • @Marconciliosouza sorry, I did not understand what I should do..

Show 4 more comments

1 answer

0


There is nothing wrong with the Entity Framework. Only your login does not have permissions.

Check with the team responsible for the database if there are any DENY for metadatabases or if login has no role public configured.

  • 1

    Thank you so much for your help! The delay in my appointment as response was due to the team that coordinates the database have taken to adjust it =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.