0
我在MVC3中使用實體框架代碼優先,用於類似博客的網站。 如果需要,我很樂意重新設計結構,如果能得到一些幫助將會很棒。如何解決EF中的這種循環引用?
的情況下被設置爲:
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;
}
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);
}
}
* 注:*我試圖擺脫WillCascadeOnDelete();
別人的建議對SO,但迄今沒有奏效。
的車型有:
會員類有關於由會員提出的帖子和回覆信息。
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; }
}
有關於誰張貼和回覆的貼吧會員信息發佈類。
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; }
}
Reply類具有關於發佈它的成員以及發佈它的帖子的信息。
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; }
}
感謝您的幫助:)
點上!謝謝。 – aredkid