1

I have the following table:

public class Category
{
    public int Id{ get; set; }

    public string Name { get; set; }

    public int? ParentId { get; set; }

    public Category Parent { get; set; }
}

with the following mapping:

public class CategoryMap: IEntityTypeConfiguration<Category>
{
    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder.ToTable(nameof(Category));

        builder.Property(x => x.Id).UseHiLo();

        //other properties
    }
}

Now I want to use fluentmigrator to create two new Entries. But the entries are related. The second category has the first category as a parent. How can I achieve it?

I can't use fluentmigrator, because I don't have the Id

        Insert
            .IntoTable("Category")
            .Row(new
            {
                Name = "Category1"
            });

        Insert
            .IntoTable("Category")
            .Row(new
            {
                Name = "Category2",
                ParentId = 0 //Category1?
            });

I could write a sql query, but I still don't have the Id. I could use a random Id, but it could end with a Problem in the HiLo algorithm.

Insert Into Category
(
    [Id],
    [Name],
    [ParentId]
)values(
    0, //?
    'Category1',
    null
)

I could use:

SELECT @@IDENTITY
SELECT IDENT_CURRENT('Category') 

But I use the HiLo algorithm to create the Id's. It should be possible to get the next HiLo value somehow for my insert script and use it. Or is there a better way to achieve it?

wydy
  • 173
  • 14

0 Answers0