如何创建与另一个类的多个关系作为不同的属性? [英] How to create multiple relations to another class as different properties?

查看:60
本文介绍了如何创建与另一个类的多个关系作为不同的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含多个对象的类,作为与另一个表的关系.

I want to create class that contains multiple objects as a relation to another table.

例如,如果我想使用不同的对象而不是集合来实现一个由三个玩家组成的团队,那么我的代码将如下所示:

For example, if I wanted to implement a team of three players using different objects instead of a collection, my code would've looked like this:

public class Team
{
    [Key]
    public int TeamId { get; set; }

    public string Name { get; set; }

    [ForeignKey("Goalkeeper")]
    [Required]
    public int GoalkeeperId { get; set; }
    public virtual Player Goalkeeper { get; set; }

    [ForeignKey("Defender")]
    [Required]
    public int DefenderId { get; set; }
    public virtual Player Defender { get; set; }

    [ForeignKey("Striker")]
    [Required]
    public int StrikerId { get; set; }
    public virtual Player Striker { get; set; }
}

public class Player
{
    [Key]
    public int PlayerId { get; set; }

    public string Name { get; set; }

    [ForeignKey("Team")]
    public int TeamId { get; set; }
    public virtual Team Team { get; set; }
}

尝试根据此代码创建迁移,但出现错误:

Trying to create a migration based on this code I'm getting an error:

无法确定类型为团队"的导航属性"Player.Team"所表示的关系.要么手动配置关系,要么使用"[NotMapped]"属性或"OnModelCreating"中的"EntityTypeBuilder.Ignore"忽略此属性.

Unable to determine the relationship represented by navigation property 'Player.Team' of type 'Team'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

我想念什么?这甚至是实现此目的的正确方法吗?

What am I missing? Is this even a correct approach to implement this?

推荐答案

我认为问题在于EF Core无法自动检测配置实体之间映射的正确方法.

I think that the problem is that EF Core cannot automatically detect what's the correct way of configure the mapping between the entities.

文档:

当在两种类型之间定义了多个导航属性时(即,不止一对指向彼此的导航),导航属性所表示的关系是不明确的.您将需要手动配置它们以解决歧义.

When there are multiple navigation properties defined between two types (that is, more than just one pair of navigations that point to each other) the relationships represented by the navigation properties are ambiguous. You will need to manually configure them to resolve the ambiguity.

因此,您应该尝试在 DbContext 中手动配置它.可能会像这样,但由于未进行测试,所以我可能在某个地方犯了一个错误.

Therefore, you should try to configure it manually in your DbContext. It would be something ish like this, but I might have a mistake somewhere since I didn't test it.

public class YourDatabaseContext : SqlContext
    {
        ...

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Team>()
                .HasOne(x => x.Goalkeeper )
                .WithOne()
                .HasForeignKey<Team>(t => t.GoalkeeperId);

            modelBuilder.Entity<Team>()
                .HasOne(x => x.Defender)
                .WithOne()
                .HasForeignKey<Team>(t => t.DefenderId);

            modelBuilder.Entity<Team>()
                .HasOne(x => x.Striker)
                .WithOne()
                .HasForeignKey<Team>(t => t.StrikerId);
        }
    }

PD:我通常更喜欢这种使用FluentAPI配置关系的方法,而不是用Attributes污染模型.为了进一步组织代码,您可以创建一个扩展每个实体的类,每个类将 IEntityTypeConfiguration< SomeEntity> 放入放置配置代码的实体,然后在 DbContext.OnModelCreating 中添加此类行以扫描您的装配并添加所有配置:

PD: I usually prefer this method of configuring the relationships with the FluentAPI instead of polluting the model with Attributes. To further organize the code, you can create a class extending IEntityTypeConfiguration<SomeEntity> per entity where you put the configuration code, and then in your DbContext.OnModelCreating you can add this line to scan your assembly an add all configurations:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        }

这篇关于如何创建与另一个类的多个关系作为不同的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆