gpt4 book ai didi

c# - 如何使 M :N (many-to-many) relationship where both M and N are the same entities?

转载 作者:行者123 更新时间:2023-11-30 17:25:55 26 4
gpt4 key购买 nike

我在使用 Entity Framework 时遇到困难。我正在尝试使用通过连接表引用其对立面的实体来设置数据模型。

我创建了实体Style和连接表StyleXStyle。

[Table("Styles")]
public class Style : FullAuditedEntity
{
public virtual string Name { get; set; }

public virtual string ShortName { get; set; }

public List<StyleXStyle> Opposites { get; set; } = new List<StyleXStyle>();
}
[Table("StylesXStyles")]
public class StyleXStyle: FullAuditedEntity
{
public virtual int StyleId { get; set; }
public Style Style { get; set; }

public virtual int OppositeId { get; set; }
public Style Opposite { get; set; }
}

当我尝试添加数据库迁移时,出现此错误:

Unable to determine the relationship represented by navigation property 'Style.Opposites' of type 'List<StyleXStyle>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

最佳答案

由于您需要自己引用 Style 实体的集合,因此您应该为这种多对多映射设置 2 个集合属性。一个导航到你的对立面,一个导航到“相似”。对于我的示例,我包含了第二个名为 Alikes 的列表属性。

有了它,您可以包含一个自定义映射来告诉 EF 它们是如何相关的:

使用基于属性的配置:

public class Style : FullAuditedEntity
{
[InverseProperty("Style")]
public List<StyleXStyle> Alikes { get; set; } = new List<StyleXStyle>();
[InverseProperty("Opposite")]
public List<StyleXStyle> Opposites { get; set; } = new List<StyleXStyle>();
}

public class StyleXStyle : FullAuditedEntity
{
[ForeignKey("Opposite")]
public virtual int OppositeId { get; set; }
public Style Opposite { get; set; }

[ForeignKey("Style")]
public virtual int StyleId { get; set; }
public Style Style { get; set; }
}

使用 Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// configures one-to-many relationship
modelBuilder.Entity<StyleXStyle>()
.HasRequired<Style>(s => s.Style)
.WithMany(g => g.Alikes)
.HasForeignKey<int>(s => s.StyleId);

// configures one-to-many relationship
modelBuilder.Entity<StyleXStyle>()
.HasRequired<Style>(s => s.Opposite)
.WithMany(g => g.Opposites)
.HasForeignKey<int>(s => s.OppositeId);
}

上面对连接实体/表执行映射,因此您可以将其用作多对多关系。如果您需要跟踪有关关系本身的任何信息(例如,CreatedDate、CreatedBy、RelationDate 等),这是必需的,但是,如果您不需要关于关系的其他信息,EF 会提供自己的方式来按照约定映射此信息关系实体。

在后一种情况下,您可以将您的实体直接相互映射并完全省略关系表。这是一个简单的例子:

[Table("Styles")]
public class Style : FullAuditedEntity
{
public virtual string Name { get; set; }

public virtual string ShortName { get; set; }

public ICollection<Style> Opposites { get; set; }
public ICollection<Style> Alikes { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

modelBuilder.Entity<Style>()
.HasMany<Style>(s => s.Alikes)
.WithMany(c => c.Opposites)
.Map(cs =>
{
cs.MapLeftKey("OppositeId");
cs.MapRightKey("StyleId");
cs.ToTable("StyleXStyle");
});

}

上面的代码是为您的示例组合在一起的,我没有对其进行全面测试,但之前已经以相同的方式实现了它。

HTH

关于c# - 如何使 M :N (many-to-many) relationship where both M and N are the same entities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com