C# 如何使用fluent api为外键指定默认值?

C# 如何使用fluent api为外键指定默认值?,c#,asp.net,database,entity-framework,C#,Asp.net,Database,Entity Framework,我在dbcontext(ToDo,Category)中使用fluent api创建了两个模型。我想要实现的是,每当我创建一个新的ToDo对象时,CategoryId的默认值被指定为我之前成功创建的“默认”类别的id,我仍然需要该关系是非可选的,以便能够使用级联删除,但是,如果没有在请求正文中指定,categoryid的值始终指定为1 我现在遇到的问题是,如果不指定categoryid,只会得到一个空值。 以下是请求和随后的响应: 发布到api/todos { "Context

我在dbcontext(ToDo,Category)中使用fluent api创建了两个模型。我想要实现的是,每当我创建一个新的ToDo对象时,CategoryId的默认值被指定为我之前成功创建的“默认”类别的id,我仍然需要该关系是非可选的,以便能够使用级联删除,但是,如果没有在请求正文中指定,categoryid的值始终指定为1

我现在遇到的问题是,如果不指定categoryid,只会得到一个空值。 以下是请求和随后的响应: 发布到api/todos


{
    "Context":"234234"
    
}




{
    "id": 2,
    "context": "234234",
    "done": false,
    "dueDate": "0001-01-01T00:00:00",
    "category": null
}
公共类AppDbContext:DbContext
{
公共数据库集类别{get;set;}
公共DbSet todos{get;set;}
公共AppDbContext(DbContextOptions选项):基本(选项){}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity().ToTable(“TODO”);
builder.Entity().HasKey(p=>p.Id);
builder.Entity().Property(p=>p.Id).IsRequired().ValueGeneratedOnAdd();
builder.Entity().Property(p=>p.Context).HasMaxLength(50);
builder.Entity().Property(p=>p.Done);
builder.Entity().Property(p=>p.DueDate);
builder.Entity().Property(p=>p.CategoryId).HasDefaultValue(1);
builder.Entity().ToTable(“类别”);
builder.Entity().HasKey(p=>p.Id);
builder.Entity().Property(p=>p.Id).IsRequired().ValueGeneratedOnAdd();//.HasValueGenerator();
builder.Entity().Property(p=>p.Name).IsRequired().HasMaxLength(30);
builder.Entity();
builder.Entity().HasData(
新的
{
Id=1,
Name=“默认值”
}
);
}
}
}

在查看评论和搜索web后,我可以清楚地说问题确实在于inmemorydb的使用,因为切换到localdb后一切正常。

您是否尝试在旧数据上实现它?我不确定它是否会有任何影响,但在您声明您有一个类别中的数据后,请尝试放置ToDo实体生成器。你正在迁移到这个位置吗?不,我正在使用inmemorydb,所以每次我重新启动应用程序时,数据都会被清除,在数据种子设定之后,把任务放在inmemorydb上无助于解决你的问题,因为inmemorydb不检查常量,所以可能你真的没有这些数据。尝试将其切换到SQLLite或SQL,看看是否会引发错误。如果是这样,那么您确定您进行了相应的迁移吗?我可以确认您的代码可以与SQLite一起工作,但不能与InMemory一起工作。
  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"

      }

  );
        }
    }
}