Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-代码优先-允许多个实体引用单个实体_C#_Entity Framework - Fatal编程技术网

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

C# 实体框架-代码优先-允许多个实体引用单个实体,c#,entity-framework,C#,Entity Framework,我一直试图先使用EF代码为我正在从事的项目创建和管理我的数据库。然而,我遇到了一个小问题 public class Planet { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } [Required] public string Planet_Name { get; set; } [Required] public int

我一直试图先使用EF代码为我正在从事的项目创建和管理我的数据库。然而,我遇到了一个小问题

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; }
}
公共级行星
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
公共int ID{get;set;}
[必需]
公共字符串Planet_Name{get;set;}
[必需]
公共整型行星{get;set;}
[必需]
公共整型行星{get;set;}
[必需]
公共字符串Planet_Desc{get;set;}
公共虚拟ICollection矿物{get;set;}
}
公共部分类矿物
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
公共int ID{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串符号{get;set;}
[必需]
公共字符串Mineral_Desc{get;set;}
[必需]
公共整数速率{get;set;}
[必需]
公共十进制比率{get;set;}
}
当使用上述方法时,Mineral表将获取一个设置为外键的Planet_Id列。当然,这会产生副作用,当两颗行星拥有相同的矿物时,会导致抛出错误。我需要的是让多个行星共享一种矿物。虽然行星需要知道它有什么矿物,但矿物没有理由知道它在什么行星上


因此,我的问题是,我如何着手这样做?(注意:我试图将一个公共虚拟行星添加到矿物类中,但没有任何改变。)

您需要在
矿物类中添加一个
ICollection 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; }        
}
因此,在DbContext中,您需要定义实体
Planet
Mineral
,并通过重写
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"));
    }
}
公共类PlanetContext:DbContext
{
公共数据库集{get;set;}
公共数据库集{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(p=>p.Minerals)
.有许多(m=>m.行星)
.Map(t=>t.MapLeftKey(“PlanetID”)
.MapRightKey(“矿质”)
.ToTable(“PlanetMineral”);
}
}

您需要多对多关系;例如,有很多教程可以使用,我感谢大家。还有一个小问题:有没有办法防止我的代码因为关系属性而变得臃肿?我可以想出至少3个其他类将使用矿物。您将始终必须使用EF定义多对多关系。看看EF公约。否则,我不确定您是否必须指定配置的.Map部分。
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"));
    }
}