1

I have an exsiting entity class, in C#, I'm using code first migration with entity framework core, and I would like to set a default value to my column with data annotation method (I have to use this, I can't use the Fluent API):

        [DefaultValue(true)]
        public bool? ColumnName { get; set; }

After I run the migration tool, the Up looks like this:

            migrationBuilder.AlterColumn<bool>(
                name: "ColumName",
                schema: "SCHEMA",
                table: "TableName",
                type: "bit",
                nullable: true,
                oldClrType: typeof(bool),
                oldType: "bit",
                oldNullable: true,
                oldDefaultValue: true);

How can I solve this problem with Data Annotation?

bugesz
  • 75
  • 1
  • 7
  • Does this help: https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value ? – YungDeiza Feb 08 '23 at 13:18
  • Firstly, you can use the Fluent API, I believe in you! Secondly, you can manually edit the migration code. See [Customize migration code](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#customize-migration-code). – Alexander Petrov Feb 08 '23 at 13:24

0 Answers0