'Returning ID from not yet created linked table index

I'm currently working on the database integration of my internship project using EntityFrameworkCore.Jet version 3.11. I cant upgrade because I'm using an MS Access database I'm facing issues when creating a row in my databse that needs to recover infos from a precedently created row in another table.

I need, from the subclass, return the ID, and adapt it to my table.

My question is: For the Condition class, the Owner should be the ID of the Node class, same for the Comment class, but with the correct ID. This should then be added to the correct table, but how should I procede, or is this doable?

To explain the way my classes work my main object is a Procedure, that holds one or many ProcedureNode, each Procedurenode has a StatementNode that inherits from the ProcedureNod

My classes are like this

public enum NodeType { 
        Condition = 0,
        Comment = 9
    }

public class Procedure 
    {
        public int Id { get; set; }
        public int? Type { get; set; } = null;
        public string Name { get; set; } = "Name";
        public string Description { get; set; } = "Description";
        public virtual List<Node> Nodes{ get; set; } = new();
        public virtual List<Comment> Comments { get; set; } = new();
        public virtual List<Condition> Conditions { get; set; } = new();
    }

public abstract class Node 

    { 
        public int? Id { get; set; } = null;
        public int? Owner { get; set; } = null;
        public int? Level { get; set; } = 0;
        public  Procedure Procedure { get; set; } = null;
    }

public class Condition 
    {

        public int? Id { get; set; } = null;
        public string Expression { get; set; } = "";
        public ConditionNode? ConditionNode { get; set; }
        public Procedure? Procedure { get; set; }
        public int Owner { get; set; }
    }
public class ConditionNode :Node
    {
        public Condition? Condition{ get; set; } = new();
        public int Reference { get; set; } = (int)NodeType.Condition;

    }
public class Comment 
    {
        public int? Id { get; set; } = null;
        public string? Content { get; set; } = null;
        public CommentNode? Node { get; set; }
        public Procedure? Procedure { get; set; }
        public int Owner { get; set; }

    }

public class CommentNode : Node
    {
        public Comment Comment { get; set; } = new();
        public int Reference { get; set; } = (int)NodeType.Comment;

    }

My Config files are like this

public class ProcedureConfig : IEntityTypeConfiguration<Core.Procedure>
    {

        public void Configure(EntityTypeBuilder<Core.Procedure> builder)
        {
            builder.ToTable("Procedures")
                .HasKey(p => p.Id);
            builder.Property(p => p.Id)
                .IsRequired()
                .ValueGeneratedOnAdd()
                .HasColumnName("Id");
            builder.Property(p => p.Name)
                .IsRequired()
                .HasColumnName("Name");
            builder.Property(p => p.Description)
                .HasColumnName("Description");
            builder.Property(p => p.Type)
                .HasColumnName("Ref");

            
            builder.HasMany(n => n.Nodes)
                .WithOne(pp => pp.Procedure)
                .HasForeignKey(pp => pp.Id);
            
            builder.HasIndex(p => p.Name).IsUnique();
            builder.HasIndex(p => p.Type);
            }
        
    }
class NodeConfig : IEntityTypeConfiguration<Node>
    {
        public void Configure(EntityTypeBuilder<Node> builder)
        {
            builder.HasKey(p => p.Id);
            builder.Property(p => p.Id)
                .IsRequired()
                .ValueGeneratedOnAdd()
                .HasColumnName("Id");
            builder.Property(p => p.Level)
                .IsRequired()
                .HasColumnName("Niveau");
            builder.Property(p => p.Owner)
                .IsRequired()
                .HasColumnName("Owner");

            builder.HasOne(p => p.Procedure)
                .WithMany(p => p.Nodes)
                .HasForeignKey(p => p.Owner);


            builder.ToTable("ProcedureNodes")
               .HasDiscriminator<int>("Type")
               .HasValue<CommentNode>((int)NodeType.Comment)
               .HasValue<ConditionNode>((int)NodeType.Condition);
            builder.HasIndex(p => p.Owner);
            builder.HasIndex(p => p.Id);
            
        }
    }
public class ConditionNodeConfig : IEntityTypeConfiguration<ConditionNode>
    {
        public void Configure(EntityTypeBuilder<ConditionNode> builder)
        {
            builder.Property(x => x.Reference)
                .HasColumnName("Ref");

            builder.HasOne(x => x.Condition)
                .WithOne(a => a.ConditionNode)
                .HasForeignKey<ConditionNode>(c => c.Reference);
        }
    }
public class ConditionConfig : IEntityTypeConfiguration<Condition>
    {
        public void Configure(EntityTypeBuilder<Condition> builder)
        {
            builder.ToTable("Condition")
                .HasKey(k => k.Id);
            
            builder.Property(p => p.Id)
                .ValueGeneratedOnAdd()
                .IsRequired()
                .HasColumnName("ID");
            builder.Property(p => p.Expression)
                .IsRequired()
                .HasColumnName("Expression");
            builder.Property(o => o.Owner)
                .HasColumnName("Owner")
                .IsRequired();

            builder.HasOne(p=>p.Procedure)
                .WithMany(c => c.Conditions)
                .HasForeignKey(p => p.Owner);
        }
    }
public class CommentNodeConfig : IEntityTypeConfiguration<CommentNode>
    {
        public void Configure(EntityTypeBuilder<CommentNode> builder)
        {
            builder.Property(x => x.Reference)
                .HasColumnName("Ref");

            builder.HasOne(x => x.Comment)
                .WithOne(a => a.Node)
                .HasForeignKey<CommentNode>(c => c.Reference);
        }   
    }
public class CommentConfig : IEntityTypeConfiguration<Comment>
    {
        /// <summary>
        /// Configures the XlibCommentStatementModel
        /// </summary>
        /// <param name="builder">The builder.</param>
        public void Configure(EntityTypeBuilder<Comment> builder)
        {
            builder.ToTable("Comments")
                .HasKey(x => x.Id);
            builder.Property(x => x.Id)
                .ValueGeneratedOnAdd()
                .IsRequired()
                .HasColumnName("Id");
            builder.Property(x => x.Content)
                .IsRequired()
                .HasColumnName("Name");
            builder.Property(x => x.Owner)
                .IsRequired()
                .HasColumnName("Owner");
            
            builder.HasOne(comment => comment.Procedure)
               .WithMany(p => p.Comments)
               .HasForeignKey(comment => comment.Owner);
        }
    }

and this is my dbContext configurator

    public class XlibDBcontext : DbContext
    {
        public XlibDBcontext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Core.Procedure> ProceduresDBSet{ get; set; }

        public DbSet<Node> ProcedureNodeDBSet { get; set; }

        protected override void OnModelCreating(ModelBuilder model)
        {
            model.Entity<Core.Procedure>()
                .HasMany(p => p.Nodes)
                .WithOne(n => n.Procedure)
                .HasForeignKey(n => n.Id);

            model.Entity<Node>()
                .HasOne(p => p.Procedure)
                .WithMany(p => p.Nodes)
                .HasPrincipalKey(p => p.Id);
            
            
            model.ApplyConfiguration(new ProcedureConfig());
            model.ApplyConfiguration(new NodeConfig());
            model.ApplyConfiguration(new ConditionConfig());
            model.ApplyConfiguration(new ConditionNodeConfig());
            model.ApplyConfiguration(new CommentConfig());
            model.ApplyConfiguration(new CommentNodeConfig());
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source