怎样才能注销身份

How can I cancel the identityy

0

我从事 asp.NetCore mvc 技术工作。我能够根据 FK 和 PK 连接 2 tables,当我对 db 执行插入操作时。我的 table 的身份是高级的,发生的事情是 table 上根本没有数据,所以它给了我空白数据。 Image: enter image description here 当我添加数据时,我不更新我选择的 table 名称,我添加了另一个具有空白名称和高级身份的类别我在 OnModelCreating 5 种类型的名称和身份进展到 6如图所示

我的模特:

public Animal()
    {
        Category = new Category();
    }
    [Key]
    public int AnimalId { get; set; }
    [StringLength(50)]
    public string? Name { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Display(Name = "Birth Date")]
    public DateTime? BirthDate { get; set; }
    public string? Description { get; set; }
    public int CategoryId { get; set; }
    [StringLength(200)]
    [Display(Name = "Portrait")]
    public string PhotoUrl { get; set; } = null!;

    [ForeignKey("CategoryId")]
    [InverseProperty("Animals")]
    public virtual Category Category { get; set; } = null!;
    //[InverseProperty("Animal")]
    //public virtual ICollection<Comment> Comments { get; set; }

public partial class Category
{
    public Category()
    {
        Animals = new HashSet<Animal>();
    }

    [Key]
    //[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public int CategoryId { get; set; }
    [StringLength(50)]
    [Display(Name = "Category Name")]
    public string? Name { get; set; }

    [InverseProperty("Category")]
    public virtual ICollection<Animal> Animals { get; set; }
}

public partial class PetDb : DbContext
{
    public PetDb()
    {

    }
    public PetDb(DbContextOptions<PetDb> options)
    : base(options)
    {
    }

    public virtual DbSet<Animal> Animals { get; set; } = null!;
    public virtual DbSet<Category> Categories { get; set; } = null!;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Animal>(entity =>
        {
            entity.HasData(
            new { AnimalId = 1, Name = "Shoko", BirthDate = DateTime.Now.AddYears(-1).AddMonths(-1).AddDays(-12), Description = "Friendly and loyal", CategoryId = 1, PhotoUrl = "ShokoDog.jpg" });
        });

        _ = modelBuilder.Entity<Category>(entity =>
          entity.HasData(
           new { CategoryId = 1, Name = "Dogs" },
           new { CategoryId = 2, Name = "Cats" },
           new { CategoryId = 3, Name = "Birds" },
           new { CategoryId = 4, Name = "Rabbits" },
           new { CategoryId = 5, Name = "Hamsters" }
          )
        );

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

图像视图:enter image description here

我还不太清楚发生了什么,但我会在这里猜测...

在您的 OnModelCreating 中,您可能希望以相反的顺序添加数据。您的 Animal 有一个指向 Category 的外键,这意味着,向 Animal 添加数据时,指定的 Category 需要已经存在。

试试这个:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Create all categories first
    _ = modelBuilder.Entity<Category>(entity =>
        entity.HasData(
            new { CategoryId = 1, Name = "Dogs" },
            new { CategoryId = 2, Name = "Cats" },
            new { CategoryId = 3, Name = "Birds" },
            new { CategoryId = 4, Name = "Rabbits" },
            new { CategoryId = 5, Name = "Hamsters" }
        )
    );

    // Then create an animal with the first category above
    modelBuilder.Entity<Animal>(entity =>
    {
        entity.HasData(
            new
            {
                AnimalId = 1,
                Name = "Shoko",
                BirthDate = DateTime.Now
                    .AddYears(-1)
                    .AddMonths(-1)
                    .AddDays(-12),
                Description = "Friendly and loyal",
                CategoryId = 1,
                PhotoUrl = "ShokoDog.jpg"
            });
    });

    OnModelCreatingPartial(modelBuilder);
}