Skip to content

EF Core generates an IDENTITY column when a owned entity has an int in its composite key. #36705

@vincent1405

Description

@vincent1405

Bug description

Hello,

I am trying to create a OwnsMany relationship, where the owned entity has a composite PK:

  • The ID of its owner
  • An int value that represents the order of the owned entity inside the collection of its owner

When I run a

dotnet migrations add 

The generated migration automatically set the order column as SQL Server IDENTITY(1, 1).

I do not understand why, and in my use case, I do not want it to be an IDENTITY column, because I want to manually manage the order of the items in the owned collection.

Please see my sample project to see the generated migration: test-owned-entity

Your code

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace test_owned_entity.Migrations
{
    /// <inheritdoc />
    public partial class InitializeDb : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Order",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Order", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OrderItem",
                columns: table => new
                {
                    OrderId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    RowIdx = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ArticleId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Quantity = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OrderItem", x => new { x.OrderId, x.RowIdx });
                    table.ForeignKey(
                        name: "FK_OrderItem_Order",
                        column: x => x.OrderId,
                        principalTable: "Order",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "OrderItem");

            migrationBuilder.DropTable(
                name: "Order");
        }
    }
}

Stack traces


Verbose output


EF Core version

9.0.8

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9.0

Operating system

Windows 10

IDE

Visual Studio Professional 2022 - 64 Bits - Version 17.14.13 (August 2025)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions