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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:51:06+00:00 2026-05-25T01:51:06+00:00

I’ve made simple classes that simulate the classes I have (sorry I had to

  • 0

I’ve made simple classes that simulate the classes I have (sorry I had to make up the classes, the usual example databases do not have the structure I wanted to ask about):

public class Animal
{  
    public System.Guid ID { get; set; }
    public string SpeciesName { get; set; }  
    public virtual ICollection<AnimalSpecies> AnimalSpecies { get; set; }
}

Species Fish:

public class Fish 
{     
    public System.Guid ID { get; set; }
    public int Freshwater { get; set; } 
}

Spieces Reptile:

public class Reptile
{     
    public System.Guid ID { get; set; }
    public int LifeExpectancy { get; set; }     
}

AnimalSpecies class:

public class AnimalSpecies
{
    public System.Guid Animal_ID { get; set; }
    public System.Guid Species_ID { get; set; }
    public virtual Animal Animal { get; set; }
} 

Mapping of the AnimalSpecies:

public AnimalSpeciesMap()
{       
    this.HasKey(t => new { t.Animal_ID, t.Spieces_ID });

    this.Property(t => t.Animal_ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    this.Property(t => t.Spieces_ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    this.ToTable("AnimalSpecies");
    this.Property(t => t.Animal_ID).HasColumnName("Animal_ID");
    this.Property(t => t.Spieces_ID).HasColumnName("Spieces_ID");

    // Relationship between Animal and AnimalSpieces: 
    this.HasRequired(t => t.Animal)
            .WithMany(t => t.AnimalSpecies)
            .HasForeignKey(d => d.Animal_ID);               
}

Since Spieces_ID doesn’t have the foreign key, is there a way to map relationship between AnimalSpecies and Fish/Reptile?

  • 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-25T01:51:07+00:00Added an answer on May 25, 2026 at 1:51 am

    I don’t think that it’s possible to define a mapping where AnimalSpecies.Species_ID participates as the foreign key in two different relationships – one between AnimalSpecies and Fish and a second between AnimalSpecies and Reptile.

    For me it looks like your model is missing a Species base class for Fish and Reptile. If you would have such a base class your model could look like this:

    public class Animal
    {
        public System.Guid ID { get; set; }
        //...
        public virtual ICollection<AnimalSpecies> AnimalSpecies { get; set; }
    }
    
    public class Species // I think the base class could also be abstract
    {
        public System.Guid ID { get; set; }
        //...
        public virtual ICollection<AnimalSpecies> AnimalSpecies { get; set; }
    }
    
    public class Fish : Species
    {
        public int Freshwater { get; set; } 
    }
    
    public class Reptile : Species
    {
        public int LifeExpectancy { get; set; }
    }
    
    public class AnimalSpecies
    {
        public System.Guid Animal_ID { get; set; }
        public System.Guid Species_ID { get; set; }
        public virtual Animal Animal { get; set; }
        public virtual Species Species { get; set; }
    }
    

    And the mapping:

    public AnimalSpeciesMap()
    {       
        this.HasKey(t => new { t.Animal_ID, t.Spieces_ID });
    
        this.Property(t => t.Animal_ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(t => t.Spieces_ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
        this.ToTable("AnimalSpecies");
    
        this.HasRequired(t => t.Animal)
            .WithMany(t => t.AnimalSpecies)
            .HasForeignKey(d => d.Animal_ID);               
    
        this.HasRequired(t => t.Species)
            .WithMany(t => t.AnimalSpecies)
            .HasForeignKey(d => d.Species_ID);               
    }
    

    If your AnimalSpecies class does not have other members than the keys and navigation properties you could also remove this class from the model and map a direct many-to-many relationship between Animal and Species (doesn’t make sense from domain viewpoint because an animal belongs only to one species, does it?):

    public class Animal
    {
        public System.Guid ID { get; set; }
        //...
        public virtual ICollection<Species> Species { get; set; }
    }
    
    public class Species // I think the base class could also be abstract
    {
        public System.Guid ID { get; set; }
        //...
        public virtual ICollection<Animal> Animals { get; set; }
    }
    
    public class Fish : Species
    {
        public int Freshwater { get; set; } 
    }
    
    public class Reptile : Species
    {
        public int LifeExpectancy { get; set; }
    }
    
    // no AnimalSpecies class anymore
    

    Mapping:

    public AnimalMap()
    {       
        this.HasMany(a => a.Species)
            .WithMany(s => s.Animals)
            .Map(x =>
            {
                x.MapLeftKey("Animal_ID");
                x.MapRightKey("Species_ID");
                x.ToTable("AnimalSpecies");
            });
    }
    

    AnimalSpecies is now a hidden table which is managed by EF for the many-to-many relationship and not exposed in the model.

    I am not sure if I understand your question correctly. This is just what came to my mind.

    Edit

    If you don’t specify any special mappings for the derived classes EF will assume TPH (Table-Per-Hierarchy) inheritance which means that all subclasses together with the base class are stored in the same database table, distinguished by a discriminator column.

    If you have many derived classes with many properties each the better inheritance strategy might be TPT (Table-Per-Type). In this case you define for each subclass its own table in the mapping:

    public FishMap()
    {
        this.ToTable("Fishes");
    }
    
    public ReptileMap()
    {
        this.ToTable("Reptiles");
    }
    

    Now every derived class gets its own table and the base class is stored in table “Species”. EF will create the appropriate joins in the database when you query for a fish for example:

    var result = context.Species.OfType<Fish>()   // Species is DbSet<Species>
        .Where(f => f.Freshwater == 1).ToList();
    

    You can read more about the different inheritance mapping strategies and their benefits and drawbacks here:

    • TPH: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

    • TPT: http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx

    • TPC: http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.