Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从一个表到多个表的一对多_C#_Asp.net Mvc - Fatal编程技术网

C# 从一个表到多个表的一对多

C# 从一个表到多个表的一对多,c#,asp.net-mvc,C#,Asp.net Mvc,如何配置从一个表(帐户)到两个表Comment和post的一对多关系 public class Account { public int AccountID { get; set; } [Required(ErrorMessage = "Please enter a username")] [Display(Name = "Username")] public string Username { get; set; } public string use

如何配置从一个表(帐户)到两个表Comment和post的一对多关系

public class Account
{
    public int AccountID { get; set; }
    [Required(ErrorMessage = "Please enter a username")]
    [Display(Name = "Username")]
    public string Username { get; set; }
    public string  userid { get; set; }
    [Required(ErrorMessage = "Please enter the password")]
    [Display(Name = "Password")]
    public string Password { get; set; }
    public ICollection<Post> Posts { get; set; }
    public ICollection<Comment> Comments { get; set; }
}


public class Post
{
    public int PostId { get; set; }
    [Required]
    public string Heading { get; set; }
    [Required]
    public string PostText { get; set; }
    public virtual Account Account {get;set;}
    public ICollection<Comment> Comments { get; set; }
}


public class Post
{
    public int PostId { get; set; }
    [Required]
    public string Heading { get; set; }
    [Required]
    public string PostText { get; set; }
    public virtual Account Account {get;set;}
    public ICollection<Comment> Comments { get; set; }
}
公共类帐户
{
public int AccountID{get;set;}
[必需(ErrorMessage=“请输入用户名”)]
[显示(Name=“Username”)]
公共字符串用户名{get;set;}
公共字符串用户标识{get;set;}
[必需(ErrorMessage=“请输入密码”)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
公共ICollection Posts{get;set;}
公共ICollection注释{get;set;}
}
公营职位
{
公共int PostId{get;set;}
[必需]
公共字符串标题{get;set;}
[必需]
公共字符串PostText{get;set;}
公共虚拟帐户{get;set;}
公共ICollection注释{get;set;}
}
公营职位
{
公共int PostId{get;set;}
[必需]
公共字符串标题{get;set;}
[必需]
公共字符串PostText{get;set;}
公共虚拟帐户{get;set;}
公共ICollection注释{get;set;}
}
下面是我的fluentapi

modelBuilder.Entity<BusinessObjects.Account>().HasMany(a => a.Posts).WithRequired().Map(m => m.MapKey("AccountId"));
modelBuilder.Entity<BusinessObjects.Account>().HasMany(a => a.Comments).WithRequired().Map(m => m.MapKey("AccountId"));
modelBuilder.Entity<BusinessObjects.Post>().HasMany(p => p.Comments).WithRequired().Map(m => m.MapKey("Postid"));
modelBuilder.Entity();
modelBuilder.Entity();
modelBuilder.Entity()有许多(p=>p.Comments).WithRequired().Map(m=>m.MapKey(“Postid”);
以下是我得到的错误:

引入外键约束 表“Posts”上的“FK_dbo.Posts_dbo.Accounts_AccountId”可能导致 循环或多个级联路径。指定ON DELETE NO ACTION或ON 不更新任何操作,或修改其他外键约束。不能 创建约束


我想我的account->post的fluent api可能不正确,有人能建议解决方案吗?

按照您的配置方式,所有关联都是必需的,这意味着:

  • 如果你删除了一个账户,你就删除了相关的帖子
  • 如果您删除一个帐户,您将删除所有相关的注释
  • 如果你删除了一篇文章,你会级联删除所有评论
因此,如果删除一个帐户,它有多个路径:

  • 它可以删除所有相关帖子,然后删除所有相关评论
  • 它可以删除所有相关的评论
这会产生不一致性,因为SQL Server不知道在删除帐户时要做什么

为了解决这个问题,您应该考虑用“代码”>“Opthual())/<代码>替换“<代码> > < <代码> >,如果这对您有意义,然后再试一次。

例如,考虑:

modelBuilder.Entity<BusinessObjects.Account>().HasMany(a => a.Posts).WithOptional().Map(m => m.MapKey("AccountId"));
modelBuilder.Entity<BusinessObjects.Account>().HasMany(a => a.Comments).WithOptional().Map(m => m.MapKey("AccountId"));
modelBuilder.Entity<BusinessObjects.Post>().HasMany(p => p.Comments).WithRequired().Map(m => m.MapKey("Postid"));
modelBuilder.Entity();
modelBuilder.Entity()有许多(a=>a.Comments).WithOptional().Map(m=>m.MapKey(“AccountId”);
modelBuilder.Entity()有许多(p=>p.Comments).WithRequired().Map(m=>m.MapKey(“Postid”);

或者,您可以使用
禁用级联删除。WillCascadeOnDelete(false)

我已经编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。