简体   繁体   中英

Entity Framework - Code First - Allowing Multiple Entities to reference a single entity

I have been attempting to use EF Code First to create and manage my Database for a project I am working on. I have, however, encountered a slight issue.

public class Planet
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required]
    public string Planet_Name { get; set; }
    [Required]
    public int Planet_X { get; set; }
    [Required]
    public int Planet_Y { get; set; }
    [Required]
    public string Planet_Desc { get; set; }
    public virtual ICollection<Mineral> Minerals { get; set;}
}

public partial class Mineral
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Symbol { get; set; }
    [Required]
    public string Mineral_Desc { get; set; }
    [Required]
    public int rate { get; set; }
    [Required]
    public decimal ratio { get; set; }
}

When using the above, The Mineral table acquires a Planet_Id Column Set as a ForeignKey. This, of course, has the side-effect of causing an error to be thrown when 2 planet have the same mineral. What I need is to allow multiple planets to share a Mineral. While the Planet needs to know what Minerals it has, the Mineral has no reason to know what Planets it is on.

My question therefore is, how do I go about doing that? (Note: I have attempted to add a public virtual Planet to the Mineral class, it changes nothing.)

you need to add a ICollection<Planet> Planets in your Mineral class :

public class Mineral
{
    public Mineral()
    {
        Planets = new HashSet<Planet>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Symbol { get; set; }
    [Required]
    public string Mineral_Desc { get; set; }
    [Required]
    public int rate { get; set; }
    [Required]
    public decimal ratio { get; set; }

    public virtual ICollection<Planet> Planets { get; set; }        
}

Also in your Planet class, you should add a default constructor :

public class Planet
{
    public Planet()
    {
        Minerals = new HashSet<Mineral>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required]
    public string Planet_Name { get; set; }
    [Required]
    public int Planet_X { get; set; }
    [Required]
    public int Planet_Y { get; set; }
    [Required]
    public string Planet_Desc { get; set; }

    public virtual ICollection<Mineral> Minerals { get; set; }
}

So In your DbContext, you need to define both entity Planet and Mineral and create a many to many relation by overriding the OnModelCreating function:

public class PlanetContext : DbContext
{
    public DbSet<Planet> Peoples { get; set; }

    public DbSet<Mineral> Minerals { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Planet>()
            .HasMany(p => p.Minerals)
            .WithMany(m => m.Planets)
            .Map(t => t.MapLeftKey("PlanetID")
                .MapRightKey("MineralID")
                .ToTable("PlanetMineral"));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM