[英]EF Core Relationships
I have problem constructing my relationships. 我在建立人际关系方面遇到问题。
I want every Post
to has one User and many Comments
. 我希望每个
Post
都有一个用户和很多Comments
。 Every User
to have many Comments
and Posts
(if I want anytime to filter comments and posts per User). 每个
User
都有很多Comments
和Posts
(如果我想随时过滤每个用户的评论和帖子)。 And then every Comment
to has one User and Post 然后每个要发表
Comment
用户都有一个
The problem is when I seed the db 问题是当我播种数据库时
if (!ctx.Posts.Any()) {
Tag tag = new Tag() {
Name = "Test1"
};
User user = new User() {
UserName = "TestUser",
Email = "test1@test1.bg",
Password = Extensions.PasswordHash("abv123456", config["AppSettings:PasswordSalt"]),
Role = UserRoles.Member
};
ctx.Users.Add(user);
Post post = new Post() {
Title = "Test Post",
Description = "Description post test",
Likes = 5,
User = user,
CreateOn = DateTime.Now,
LastEditOn = DateTime.Now
};
PostTag posttags = new PostTag() {
Post = post,
Tag = tag
};
ctx.PostTag.Add(posttags);
ctx.SaveChanges();
};
And If I want to fetch all posts with the related User
如果我想获取相关
User
所有帖子
public IActionResult GetAllPost() {
var posts = _ctx.Posts
.Include(u => u.User)
.ToList();
return Json(Newtonsoft.Json.JsonConvert.SerializeObject(posts));
}
I get Newtonsoft.Json.JsonSerializationException: Self referencing loop detected with type 'Blog.Data.Entities.Post'. 我得到Newtonsoft.Json.JsonSerializationException:检测到自引用循环,类型为'Blog.Data.Entities.Post'。 Path '[0].User.Posts'.
路径“ [0] .User.Posts”。
While in my Configure
method I did ignore it 在我的
Configure
方法中,我确实忽略了它
services.AddMvc().AddJsonOptions(opts => {
opts.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
OnModelCreating() OnModelCreating()
protected override void OnModelCreating(ModelBuilder modelBuilder) {
// set unique email and username
modelBuilder.Entity<User>()
.HasIndex(u => new { u.Email, u.UserName })
.IsUnique();
// set unique tag name
modelBuilder.Entity<Tag>()
.HasIndex(t => t.Name)
.IsUnique();
modelBuilder.Entity<Post>()
.HasOne(p => p.User)
.WithMany(u => u.Posts);
modelBuilder.Entity<User>()
.HasMany(u => u.Comments)
.WithOne(u => u.User);
// post tag many-to-many rel
modelBuilder.Entity<PostTag>()
.HasKey(pt => new { pt.PostUID, pt.TagUID });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostUID);
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.TagUID);
}
You are trying to JSONify a JSON string: 您正在尝试对JSON字符串进行JSON化:
return Json(Newtonsoft.Json.JsonConvert.SerializeObject(posts));
You need to let the framework do it for you, instead: 您需要让框架为您做到这一点,而不是:
return Json(posts);
The problem is you've configured your service, but you're not using the service. 问题是您已经配置了服务,但没有使用该服务。 You're using a static reference to Newtonsoft Json converter.
您正在使用对Newtonsoft Json转换器的静态引用。 You either need to use the service provided by framework (it should use it automatically when returning a model from a controller), or apply the configuration to the Newtonsoft Json converter call that you're using.
您或者需要使用框架提供的服务(从控制器返回模型时,它应该自动使用该服务),或者将配置应用于正在使用的Newtonsoft Json转换器调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.