[英]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中,你需要定义实体
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"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.