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 8515281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:06:18+00:00 2026-06-11T05:06:18+00:00

I have a code first application which defined like this: public abstract class Entity

  • 0

I have a code first application which defined like this:

public abstract class Entity
{
    [Key]
    public int Id { get; set; }

    public DateTime CreatedOn { get; set; }
}

public class Post : Entity
{
    public string Text { get; set; }
    public virtual ICollection<UserObject> Likes { get; set; }
} 

public class Blog : Post
{
    public string Title { get; set; }
    public string Content { get; set; }
}

public class Comment : Post
{
    public string Content { get; set; }
    public virtual Post Parent { get; set; }
}

public class UserObject : Entity
{
    public string Username { get; set; }
    public string Login { get; set; }
}

public class Profile : UserObject
{
    public DateTime DoB { get; set; }
    public string Avatar { get; set; }
    public ICollection<Blog> Blogs { get; set; } 
}

The idea is: A profile can have many blogs, a post (blog or comment) can have many likes. I want something like this on database:

Table Posts

Id
...

Table Profiles

Id
...

Table PostLikes

Id
PostId
UserId

Table ProfileBlogs

Id
UserId
BlogId

I tried but can’t get Fluent API to generate these schemes. I tried with many-to-many relations, but because my data structure has inheritances, so it does not work.

How to do this in Fluent API?

  • 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-06-11T05:06:19+00:00Added an answer on June 11, 2026 at 5:06 am

    Here’s a schema that worked for me:

    The fluent mapping:

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Profile>()
                    .Map(m => m.ToTable("Profiles"));
    
                modelBuilder.Entity<Post>()
                    .HasMany(p => p.Likes)
                    .WithMany()
                    .Map(m =>
                        {
                            m.ToTable("PostLikes");
                            m.MapLeftKey("PostId");
                            m.MapRightKey("UserId");
                        });
    
                modelBuilder.Entity<Profile>()
                    .HasMany(p => p.Blogs)
                    .WithMany()
                    .Map(m =>
                    {
                        m.ToTable("ProfileBlogs");
                        m.MapLeftKey("UserId");
                        m.MapRightKey("BlogId");
                    });
            }
    

    Created this database:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Entity in Code First Entity framework that currently looks like this:
For an application using Code First EF 5 beta I have: public class ParentObject
I have some questions with relationship of ef code first. My code: public class
I'm using the Code First approach and have the following Model: public class Person
I have this formula in a cell: =GetData(Channel_01,Chicago) Which executes this code: Public Function
I have a code like this public static Type ToType(XmlSerializableType xmlSerializableType) { string func
I have implemented the code in Devise call backs in my application controller. First
I have a simple code in Entity Framework (EF) v4.1 code first: PasmISOContext db
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have code like that 010200345 i want split every first three digit please

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.