Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:12:09+00:00 2026-05-24T17:12:09+00:00

I am using Entity Framework Code First with SQLCE in MVC3 for a blog-like

  • 0

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;
    }

    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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:12:10+00:00Added an answer on May 24, 2026 at 5:12 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
I'm working on an MVC site with Entity Framework Code First, using Ninject for
I have been using Entity Framework CTP with Code-First as in this tutorial by
I'm using code-first in entity-framework 4, Entity Framework Feature CTP 3 Is it possible
How to declare a one to one relationship using Entity Framework 4 Code First
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I am using Entity Framework Code First and ran into a small road block.
Using the Entity Framework Code First paradigm I have defined the following objects and
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
Using Entity Framework Code First CTP5, how do I create a primary key column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.