简体   繁体   中英

One to many from one table to multiple tables

How do i go about configuring a one to many relationship from one table (Account) to two tables Comment and 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; }
}

following is my fluent api

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"));

Following is the error i get:

Introducing FOREIGN KEY constraint 'FK_dbo.Posts_dbo.Accounts_AccountId' on table 'Posts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint

I suppose my fluent api for account -> post might not be correct, can anyone suggest a solution?

The way you configured it, all associations are required, that means that:

  • If you delete an Account, you cascade delete related Posts.
  • If you delete an Account, you cascade delete all related Comments.
  • if you delete a Post, you cascade delete all Comments.

Hence, if you delete an Account, it has multiple paths:

  • It can delete all related posts and then all related comments
  • It can delete all related comments

This generates an inconsistency, because SQL Sever doesn't know exactly what to do when you delete an Account.

To solve that, you should consider replacing some WithRequired() with WithOptional() , if that makes sense for you, and then trying again.

For instance, consider:

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"));

Alternatively, you can disable cascade delete with .WillCascadeOnDelete(false) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM