How to assign default value to a foreign key using fluent api?

I created two models using fluent api in my dbcontext(ToDo, Category) what I want to achieve is that whenever I create a new ToDo object, the default value for CategoryId is specified as an id for the ‘Default’ category that I successfully created previously, I still need that relationship to be non optional to be able to use cascade deletion, but so that if not specified in a request body, the value of categoryid always assigns to 1.

The issue I’m having now is that when not specifying the categoryid simply gets a null value.
Here are the request and the respond that followed it:
POST to api/todos

{
    "Context":"234234"
    
}




{
    "id": 2,
    "context": "234234",
    "done": false,
    "dueDate": "0001-01-01T00:00:00",
    "category": null
}

  public class AppDbContext : DbContext
    {
        public DbSet<Category> categories { get; set; }
        public DbSet<ToDo> todos { get; set; }

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

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

        builder.Entity<ToDo>().ToTable("TODO");
            builder.Entity<ToDo>().HasKey(p => p.Id);
            builder.Entity<ToDo>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd();
            builder.Entity<ToDo>().Property(p => p.Context).HasMaxLength(50);
            builder.Entity<ToDo>().Property(p => p.Done);
            builder.Entity<ToDo>().Property(p => p.DueDate);
            builder.Entity<ToDo>().Property(p => p.CategoryId).HasDefaultValue(1);

            builder.Entity<Category>().ToTable("CATEGORIES");
            builder.Entity<Category>().HasKey(p => p.Id);
            builder.Entity<Category>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd();//.HasValueGenerator<InMemoryIntegerValueGenerator<int>>();
            builder.Entity<Category>().Property(p => p.Name).IsRequired().HasMaxLength(30);
            builder.Entity<Category>().HasMany(p => p.ToDoS).WithOne(p => p.Category).HasForeignKey(p => p.CategoryId);

            builder.Entity<Category>().HasData(
      new
      {
          Id = 1,
          Name = "Default"

      }

  );
        }
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

After reviewing the comments and searching the web I can clearly say that the problem really was the usage of inmemorydb , cause after switching to localdb everything works just fine.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x