开发者

How do I get around this cyclical reference in EF?

I am using Entity Framework Code First with SQLCE in MVC3 for a blog-like site. I am open to redesigning the structure if required, it would be great to get some help.

The context is set up as:

public class BinarContext : DbContext
{
    public DbSet<Member> Members { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<Reply> Replies { get; set; }

    public BinarContext()
    {
        this.Configuration.LazyLoadingEnabled = true;
    }

 开发者_JS百科   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Member>().HasMany(member => member.Posts)
                                     .WithRequired(post => post.Member)
                                     .HasForeignKey(post => post.MemberID)
                                     .WillCascadeOnDelete();

        modelBuilder.Entity<Member>().HasMany(member => member.Replies)
                                     .WithRequired(reply => reply.Member)
                                     .HasForeignKey(reply => reply.MemberID)
                                     .WillCascadeOnDelete();

        modelBuilder.Entity<Post>().HasMany(post => post.Replies)
                                   .WithRequired(reply => reply.Post)
                                   .HasForeignKey(reply => reply.PostID)
                                   .WillCascadeOnDelete();

        base.OnModelCreating(modelBuilder);
    }
}

*Note: * I have tried getting rid of WillCascadeOnDelete(); as suggested by others on SO but hasn't worked so far.

The models are:

The Member class has information about the posts and replies made by the member.

public class Member
{
    public Guid ID {get; set;}
    public string Username { get; set; }
    public string Email { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Reply> Replies { get; set; }
}

The Post class that has information about the member who posted it and for the replied posted for it.

public class Post
{
    public Guid ID {get; set;}
    [DataType(DataType.MultilineText)]
    public string Text { get; set; }
    public Guid MemberID { get; set; }

    public virtual Member Member { get; set; }
    public virtual ICollection<Reply> Replies { get; set; }
}

The Reply class that has information about the member who posted it and for the post it was posted.

public class Reply    
{
    public Guid ID { get; set; }
    [DataType(DataType.MultilineText)]
    public string Text { get; set; }

    public Guid PostID { get; set; }
    public Guid MemberID { get; set; }

    public virtual Post Post { get; set; }
    public virtual Member Member { get; set; }
}

Thanks for your help :)


Try this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Member>().HasKey(x=>x.ID)
                                     .HasMany(x => x.Posts)
                                     .WillCascadeOnDelete();

        modelBuilder.Entity<Member>().HasKey(x=>x.ID)
                                     .HasMany(x => x.Replies)
                                     .WillCascadeOnDelete();

        modelBuilder.Entity<Post>().HasKey(x=>x.ID)
                                   .HasMany(x => x.Replies)
                                   .WillCascadeOnDelete();

        modelBuilder.Entity<Post>().HasKey(x=>x.ID)
                                   .WithRequired(x => x.Member)
                                   .WithMany(x=>x.Posts);

        modelBuilder.Entity<Replies>().HasKey(x=>x.ID)
                                   .WithRequired(x => x.Member)
                                   .WithMany(x=>x.Replies);

        modelBuilder.Entity<Replies>().HasKey(x=>x.ID)
                                   .WithRequired(x => x.Post)
                                   .WithMany(x=>x.Replies);

        base.OnModelCreating(modelBuilder);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜