0

i have model class like below

public class DIMAuthOffice
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public string Title { get; set; }
        [StringLength(50)]
        [Required]
        public string UserNameManager { get; set; }
        public int? ParentID { get; set; }
        [ForeignKey(nameof(ParentID))]
        public DIMAuthOffice OfficeParent { get; set; }
        public List<DIMAuthOffice> OfficeChilds { get; set; } = new List<DIMAuthOffice>();

        [ForeignKey(nameof(UserNameManager))]
        public DIMAuthUser UserManager { get; set; }
        public List<DIMAuthRole> DIMAuthRoles { get; set; } = new List<DIMAuthRole>();
        public List<DIMAuthRolePostOffice> DIMAuthRolePostOffices { get; set; } = new List<DIMAuthRolePostOffice>();

    }

but when i execute

Add-Migration

the generated File is :

migrationBuilder.CreateTable(
                name: "DIMAuthOffices",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Title = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    UserNameManager = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    ParentID = table.Column<int>(type: "int", nullable: false),
                    UserManagerUserName = table.Column<string>(type: "nvarchar(50)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DIMAuthOffices", x => x.ID);
                    table.ForeignKey(
                        name: "FK_DIMAuthOffices_DIMAuthOffices_ParentID",
                        column: x => x.ParentID,
                        principalTable: "DIMAuthOffices",
                        principalColumn: "ID");
                    table.ForeignKey(
                        name: "FK_DIMAuthOffices_DIMAuthUsers_UserManagerUserName",
                        column: x => x.UserManagerUserName,
                        principalTable: "DIMAuthUsers",
                        principalColumn: "UserName");
                });

wrong line is :

ParentID = table.Column(type: "int", nullable: false),

the generated File is Wrong and property ParentID set to nullable:false but this should be true ... why ???

1
  • 1
    Try to declare public DIMAuthOffice? OfficeParent { get; set; } Feb 7, 2022 at 14:36

2 Answers 2

1

Doesn't repro for me on EF Core 6. eg

public class DIMAuthOffice
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string Title { get; set; }
    [StringLength(50)]
    [Required]
    public string UserNameManager { get; set; }
    public int? ParentID { get; set; }
    [ForeignKey(nameof(ParentID))]
    public DIMAuthOffice OfficeParent { get; set; }
    public List<DIMAuthOffice> OfficeChilds { get; set; } = new List<DIMAuthOffice>();


}

creates an initial migration of:

    migrationBuilder.CreateTable(
        name: "DIMAuthOffice",
        columns: table => new
        {
            ID = table.Column<int>(type: "int", nullable: false)
                .Annotation("SqlServer:Identity", "1, 1"),
            Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
            UserNameManager = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
            ParentID = table.Column<int>(type: "int", nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_DIMAuthOffice", x => x.ID);
            table.ForeignKey(
                name: "FK_DIMAuthOffice_DIMAuthOffice_ParentID",
                column: x => x.ParentID,
                principalTable: "DIMAuthOffice",
                principalColumn: "ID");
        });
1

I use FluentApi and writed this code in modelBuilder and fixed issue

modelBuilder.Entity<DIMAuthOffice>()
                .HasOne(q => q.OfficeParent)
                .WithMany(q => q.OfficeChilds)
                .IsRequired(false)
                .OnDelete(DeleteBehavior.Restrict);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.