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

Defaults cannot be created on columns with an IDENTITY attribute. #5660

Closed
MicahZoltu opened this issue Jun 4, 2016 · 6 comments
Closed

Comments

@MicahZoltu
Copy link
Contributor

Steps to reproduce

  1. Create a database via code first with one table with a [Key] column and create/apply an initial migration.
  2. Add a second column like
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Int64 SecondaryId { get; set; }
  3. Create a new migration. NOTICE that the migration has defaultValue: 0L.
  4. Apply migration. NOTICE error: Defaults cannot be created on columns with an IDENTITY attribute.

The issue

I expect that creating a migration will create a valid migration, rather than generating a migration that can't be executed.

If you are seeing an exception, include the full exceptions details (message and stack trace).

System.Data.SqlClient.SqlException: Defaults cannot be created on columns with an IDENTITY attribute. Table 'Products', column 'SecondaryId'.
Could not create constraint or index. See previous errors.
   at System.Data.SqlClient.SqlConnection.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 Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandExtensions.ExecuteNonQuery(IEnumerable`1 commands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Execute(IEnumerable`1 relationalCommands)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
ClientConnectionId:f645748b-2ffa-4b53-b1b6-c85babc014da
Error Number:1754,State:0,Class:16
Defaults cannot be created on columns with an IDENTITY attribute. Table 'Products', column 'SecondaryId'.
Could not create constraint or index. See previous errors.

Further technical details

EF Core version: 1.0.0-rc2-final
Operating system: Windows 10
Visual Studio version: 2015

Other details about my project setup:
EntityFrameworkCore.Tools 1.0.0-preview1-final
Azure SQL Server for the backend.

@divega
Copy link
Contributor

divega commented Jun 7, 2016

@Zoltu we believe this issue was already fixed after RC2. Could you check with nightly builds?

@AndriySvyryd did you fix this one? Could you please find the original bug and close this as a dupe? (I couldn't).

@jimnorth55
Copy link

Possible work-around... Adding ".ValueGeneratedNever()" to the fluent API configuration for the field seems to prevent EF from adding the IdentityColumn annotation.

@divega
Copy link
Contributor

divega commented Jun 8, 2016

@AndriySvyryd friendly ping... I am not sure what change would have fixed this.

@AndriySvyryd
Copy link
Member

Dupe: #5384
Fixed in 43d85bf

@mafshin
Copy link

mafshin commented Aug 6, 2016

I get this exception yet in EF Core 1.0

Defaults cannot be created on columns with an IDENTITY attribute. Table 'Languages', column 'ID'.
Could not create constraint or index. See previous errors.

    public class Language
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    migrationBuilder.AddColumn<int>(
                name: "ID",
                table: "Languages",
                nullable: false,
                defaultValue: 0)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

@ajcvickers
Copy link
Contributor

@mafshin This is likely the same issue as is being tracked by #6035

@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
None yet
Projects
None yet
Development

No branches or pull requests

6 participants