gpt4 book ai didi

c# - .HasRequired 无法在模型中工作

转载 作者:行者123 更新时间:2023-11-30 16:49:25 26 4
gpt4 key购买 nike

我一直在四处寻找答案,但找不到合适的答案。我有两个具有 1:* 关系的模型:

namespace test.Models
{
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string BloggerName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
}

namespace test.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}

还有我的ApplicationDbContext是:

namespace test.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Post> Post { get; set; }
public DbSet<Blog> Blog { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
base.OnModelCreating(builder);
builder.Entity<Post>().HasRequired(p => p.Blog);
}
}
}

但是,我得到以下错误:

CS1061: 'EntityTypeBuilder' does not contain a definition for 'HasRequiered' and no extension method 'HasRequiered' accepting a first argument of type 'EntityTypeBuilder' could be found (are you missing a using directive or an assembly reference?)

我是新手,所以一直在学习各种教程,但都推荐以这种方式使用它。任何帮助将不胜感激。我只想确保 <post>始终是博客的一部分。 HasRequiered尽管此错误始终显示为红色。

最佳答案

试试这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
builder.Entity<Post>().HasRequired(p => p.Blog);
}

关于c# - .HasRequired 无法在模型中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com