c# - Entity Framework - 代码优先 - 允许多个实体引用单个实体

标签 c# entity-framework

我一直在尝试使用 EF Code First 为我正在处理的项目创建和管理我的数据库。但是,我遇到了一个小问题。

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

使用上述方法时,矿物表获取一个 Planet_Id 列集作为外键。当然,当 2 个行星具有相同的矿物时,这会导致抛出错误。我需要的是让多个星球共享一个Mineral。虽然行星需要知道它有什么矿物,但矿物没有理由知道它在什么行星上。

因此,我的问题是,我该怎么做呢? (注意:我试图将公共(public)虚拟行星添加到矿物类中,但它没有任何改变。)

最佳答案

你需要添加一个ICollection<Planet> Planets在你的Mineral类:

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

也在你的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; }
}

因此在您的 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"));
    }
}

关于c# - Entity Framework - 代码优先 - 允许多个实体引用单个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125615/

相关文章:

c# - 如何将新输入的项目保存到组合框项目源中?

c# - 应用程序信任级别

c# - 自定义集合实现 IEnumerable

entity-framework - Entity Framework 多对多问题

c# - 使用 Azure Kinect DK 将 In32Rect 坐标转换为 Span<BGRA>

c# - 检查是否按下了 3 个键

entity-framework - Entity Framework : deleted SQL table is not removed from the model

c# - 替代多个使用 block

c# - 多对可选关系是否可能?

c# - Entity Framework 问题 "The table/view ' 没有定义主键。”