Base solution for your next web application
Open Closed

Rad tool regenerate entity issue #6149


User avatar
0
matt created

Suppose I have added entity with some field. Again I have added new fileds to that entity and trying to update database getting below error. Pelase help me on it. Is it possible to add all migraion to one file so that it should not be confusion what is going on in migration file.

PM> update-database Applying migration '20181216022015_Customer table added'. Failed executing DbCommand (625ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] DECLARE @var1 sysname; SELECT @var1 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Customer]') AND [c].[name] = N'Id'); IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Customer] DROP CONSTRAINT [' + @var1 + '];'); ALTER TABLE [Customer] ALTER COLUMN [Id] bigint NOT NULL; System.Data.SqlClient.SqlException (0x80131904): The object 'PK_Customer' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) ClientConnectionId:9bf68424-7cbb-464a-beac-a6cab2b252da Error Number:5074,State:1,Class:16 The object 'PK_Customer' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.

Erlier table name is Customer and below are my fields.

[Required] public virtual string Tags { get; set; }

	[Required]
	public virtual long CreatedBy { get; set; }
	
	[Required]
	public virtual DateTime CreatedDate { get; set; }
	
    
    Now I ahve added two more field  and want customerid will be my primary key
    
    public virtual long CustomerId { get; set; }
	
	public virtual string StatusId { get; set; }

4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @matt

    Have you tried restarting your DB Server ?

  • User Avatar
    0
    matt created

    Yes After updated my entity I deleted database and regenerate database again. But Always Id is taking as primarykey. Trying to assign customerid as primary key I am unable to do it.

  • User Avatar
    0
    matt created

    modelBuilder.Entity("ASTAR.Customers.Customer", b => { b.Property<long>("Id") .ValueGeneratedOnAdd() .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

                    b.Property&lt;long&gt;("CreatedBy");
    
                    b.Property&lt;DateTime&gt;("CreatedDate");
    
                    b.Property&lt;string&gt;("CustomerAccountNumber")
                        .IsRequired();
    
                    b.Property&lt;string&gt;("CustomerCompanyName")
                        .IsRequired();
    
                    b.Property&lt;long&gt;("CustomerId");
    
                    b.Property&lt;string&gt;("StatusId");
    
                    b.Property&lt;string&gt;("Tags")
                        .IsRequired();
    
                    b.HasKey("Id");
    
                    b.HasIndex("CustomerId");
    
                    b.ToTable("Customer");
                });
                
                I need customer id should be the primary key when RAD tool generating the migration file
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @matt

    Sorry for the delay, have you figured it out ?