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

  • Home
  • SEARCH
  • 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 7951693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:38:14+00:00 2026-06-04T02:38:14+00:00

I have the code as below. class Student : IPeople { private string name;

  • 0

I have the code as below.

  class Student : IPeople
{
    private string name;
    public string Name
    {
        get { return name;}
        set { name = value;}
    }

    private bool sex;
    public bool Sex
    {
        get{ return sex; }
        set{ sex = value;}
    }

    private int age;
    public int Age
    {
        get{return age;}
        set{age = value;}
    }

    public virtual ICollection<Dog> dogs { get;set; }

    public Student()
    {
        dogs = new List<Dog>();
    }
}

class Pet
{
    string Name { get; set; }
    bool Sex { get; set; }
    int Age{get;set;}
}

class Dog : Pet
{
    public string Type { get; set; }
    public virtual ICollection<IPeople> persons { get; set; }

    public Dog()
    {
        persons = new List<IPeople>();
    }
}

The context is

class TestContext : DbContext
{
    public DbSet<Student> studentSet { get; set; }
    public DbSet<Dog> dogSet { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().HasMany(x => x.dogs).WithMany(y => (ICollection<Student>)y.persons);
    }
}

If I insert the records likes below,

using (TestContext context = new TestContext())
        {
            Student s = new Student();
            s.Age = 18;
            s.Sex = true;
            s.Name = "ts";
            Dog d = new Dog();
            d.Type = "abc";
            d.Sex = false;
            d.Name = "dog";
            d.Age = 3;
            s.dogs.Add(d);
            context.studentSet.Add(s);
            context.SaveChanges();
        }

everything works well, but if I insert the records likes below, the Student record will not insert into database.

using (TestContext context = new TestContext())
        {
            Student s = new Student();
            s.Age = 18;
            s.Sex = true;
            s.Name = "ts";
            Dog d = new Dog();
            d.Type = "abc";
            d.Sex = false;
            d.Name = "dog";
            d.Age = 3;
            d.persons.Add(s);
            context.dogSet.Add(d);
            context.SaveChanges();
        }

Anyone can 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-06-04T02:38:16+00:00Added an answer on June 4, 2026 at 2:38 am

    You can’t use the interface IPeople here:

    public virtual ICollection<IPeople> persons { get; set; }
    

    Navigation properties must refer to entity classes – either abstract or concrete – of your model.

    A possible alternative might be to use an abstract class People instead of an interface. But you have to put the navigation property…

    public virtual ICollection<Dog> dogs { get;set; }
    

    …into that abstract class, not into the derived Student class, because Dog.persons refers to the abstract class People, something like:

    abstract class People
    {
        // ...
        public virtual ICollection<Dog> dogs { get;set; }
    }
    
    class Student : People
    {
        // ...
    }
    
    class Pet
    {
        // ...
    }
    
    class Dog : Pet
    {
        // ...
        public virtual ICollection<People> persons { get; set; }
    }
    

    And the mapping would be:

    modelBuilder.Entity<People>()
        .HasMany(x => x.dogs)
        .WithMany(y => y.persons)
        .Map(m =>
        {
            m.ToTable("PeoplesDogs");
            m.MapLeftKey("PeopleId");
            m.MapRightKey("DogId");
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below: public class PatientAgent extends Agent { private final String
Assuming the code below: public class DynamicAspxHandler : IHttpHandler { bool IHttpHandler.IsReusable { get
I have the code below : public class Anything { public int Data {
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have the Global.asax like the code below: public class MvcApplication : System.Web.HttpApplication {
I have two classes, shown below: [Serializable] [XmlInclude(typeof(SomeDerived))] public class SomeBase { public string
I have these two classes: class Student { String name; String age ; }
i have the code as below : class Singleton { private: int i; static
I have the following question regarding the code below: public class GenericBridgeMethods <T> {
I have the code below: class A { }; class B: public virtual A

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.