Asp.net 实体框架核心删除引用对象
我有这个模型Asp.net 实体框架核心删除引用对象,asp.net,entity-framework-core,Asp.net,Entity Framework Core,我有这个模型 public class Post { [Key] public int Id { get; set; } public string AuthorId { get; set; } public ApplicationUser Author { get; set; } public int ChannelId { get; set; } [Required] public Channel Channel { get; set;
public class Post
{
[Key]
public int Id { get; set; }
public string AuthorId { get; set; }
public ApplicationUser Author { get; set; }
public int ChannelId { get; set; }
[Required]
public Channel Channel { get; set; }
[Required]
public string Text { get; set; }
public int CountViewers { get; set; }
public List<ApplicationUser> WhoLiked { get; set; }
public DateTime Date { get; set; } = DateTime.Now;
}
当我将用户添加到某个帖子中的“Wholiked”列表时,EF会将该用户从其他帖子的列表中删除。
我做错了什么
post.WhoLiked.Add(user);
_db.Posts.Update(post);
await _db.SaveChangesAsync();
我做错了什么?
← 我不知道你怎么期望没有直接访问你代码的人回答这个问题。请检查并提供一个。什么是post
,它来自哪里,它包含什么?什么是更新
?这不是标准的EF 6.x方法。预期的结果是什么?实际结果是什么?此外,您有两个对ApplicationUser(whoolike&Author)的引用。这需要特殊的配置。显示模型/fluent代码。
post.WhoLiked.Add(user);
_db.Posts.Update(post);
await _db.SaveChangesAsync();