简体   繁体   English

实体框架 - 代码优先 - 允许多个实体引用单个实体

[英]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. 我一直在尝试使用EF Code First为我正在处理的项目创建和管理我的数据库。 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. 使用上述内容时,Mineral表获取Planet_Id列集作为ForeignKey。 This, of course, has the side-effect of causing an error to be thrown when 2 planet have the same mineral. 当然,当2个行星具有相同的矿物质时,这会产生导致错误的副作用。 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.) (注意:我试图将一个公共虚拟星球添加到Mineral类中,它什么都没改变。)

you need to add a ICollection<Planet> Planets in your Mineral class : 你需要在Mineral类中添加一个ICollection<Planet> Planets

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 : 同样在Planet类中,您应该添加一个默认构造函数:

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: 所以在你的DbContext中,你需要定义实体PlanetMineral并通过覆盖OnModelCreating函数来创建多对多的关系:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先关系:一对多到多个实体 - Entity Framework Code First Relationships: One to Many to Multiple Entities 实体框架代码优先-相同类型的2个实体 - Entity Framework Code First - 2 Entities of the Same Type 实体框架代码优先和链接实体 - Entity Framework Code First and linked entities 实体框架6首先编写代码-参照另一个现有实体添加实体 - Entity framework 6 code first - Add entity with reference to another existing entity 如何映射Entity Framework 5中的标识关系第一个子实体与多个互斥的父实体 - How to map identifying relationship in Entity Framework 5 code first child entity with multiple mutually exclusive parent entities 实体框架代码优先参考约束问题 - Entity Framework Code First Reference constraint issue 实体框架代码优先 - 引用相同的表 - Entity Framework Code First - Reference same table 实体框架,代码优先建模和循环引用 - Entity Framework, Code First modeling and a cyclical reference 首先使用实体​​框架代码保存单个EntityObject - Saving single EntityObject with Entity Framework code first 首先将sql视图映射到Entity Framework Code中已定义的多个实体吗? - Map a sql view to already defined multiple entities in Entity Framework Code first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM