- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在四处寻找答案,但找不到合适的答案。我有两个具有 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/
我一直在四处寻找答案,但找不到合适的答案。我有两个具有 1:* 关系的模型: namespace test.Models { public class Blog { p
我试图在 User 表和 UserSettings 表之间设置一对一关系。 尽管指定了“HasRequired”关系,但我仍然可以在没有任何设置的情况下插入用户。我哪里错了? public class
我有两个非常简单的对象: public class GenericObject { public int Id { get; set; } public string descript
使用 Entity Framework,我们在字段上设置了 HasRequired。 EF 核心的替代品是什么? https://msdn.microsoft.com/en-us/library/jj
我正在尝试通过 C# 中的代码与 Entity Framework Core 连接两个表。 类(class)是: public class roll { [key] public in
我有以下实体 public class SchoolContext : DbContext { public DbSet Addresses { get; set; }
所以在疯了 3 小时并在网上到处找之后,我强制自己在这里问。我使用 Entity Framework 已经 5 年多了,我从来没有遇到过我在下面描述的那种问题 我有两个 POCO 类(已编辑): pu
我是一名优秀的程序员,十分优秀!