简体   繁体   English

在 EF Core 中使用不同的名称指定关系

[英]Specify a relationship in EF Core with a different name

I have a problem with the following relationship in EF Core 3.1.8.我对 EF Core 3.1.8 中的以下关系有疑问。 I have the following classes:我有以下课程:

public class UnitOfMeasure
{
    public UnitOfMeasure(string Code, string Description)
    {
        this.Code = Code;
        this.Description = Description;
    }
    public int Id { get; private set; }
    public string Code { get; private set; }
    public string Description { get; private set; }
}

public class Article
{
    private Article() { }
    public Article(string Code, string Description, UnitOfMeasure BaseUnitOfMeasure)
    {
        this.Code = Code;
        this.Description = Description;
        this.BaseUnitOfMeasure = BaseUnitOfMeasure;
        this.BaseUnitOfMeasureId = BaseUnitOfMeasure.Id;
    }
    public int Id { get; private set; }
    public string Code { get; private set; }
    public string Description { get; private set; }
    public int BaseUnitOfMeasureId { get; private set; }
    public UnitOfMeasure BaseUnitOfMeasure { get; private set; }
}

internal class ArticleConfiguration : IEntityTypeConfiguration<Article>
{
    public void Configure(EntityTypeBuilder<Article> builder)
    {
        builder.HasIndex(a => a.Code)
            .IsUnique();

        builder.HasOne(a => a.BaseUnitOfMeasure)
            .WithMany()
            .HasForeignKey(a => a.BaseUnitOfMeasureId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}

public class WmsDbContext : DbContext
{
    public WmsDbContext(DbContextOptions options)
        : base(options)
    {
        ChangeTracker.LazyLoadingEnabled = false;
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            builder.ApplyConfigurationsFromAssembly(assembly);
        }

        base.OnModelCreating(builder);
    }
}

... When I run this, I'm getting the following error: : 'Unable to determine the relationship represented by navigation property 'Article.BaseUnitOfMeasure' of type 'UnitOfMeasure'. ...当我运行它时,我收到以下错误: '无法确定由'UnitOfMeasure'类型的导航属性'Article.BaseUnitOfMeasure'表示的关系。 Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。

This is driving me mad.这让我发疯。 I can't find anything to get a relationship to work.我找不到任何东西可以让关系工作。 I don't want to specify the other side of the relation on the UnitOfMeasure class, because this makes no sense in my domain.我不想在 UnitOfMeasure class 上指定关系的另一端,因为这在我的域中没有意义。

How can I add a navigation property with a different name than the class?如何添加名称与 class 不同的导航属性?

Your relationship is one UnitOfMeasure with many Articles, right?您的关系是一个包含许多文章的 UnitOfMeasure,对吗? If so you can simply add the following code to the UnitOfMeasure class:如果是这样,您只需将以下代码添加到 UnitOfMeasure class:

public ICollection<Article> Articles { get; set; }

and change the builder.HasOne like this:并像这样更改builder.HasOne:

builder.HasOne(d => d.BaseUnitOfMeasure)
       .WithMany(p => p.Articles)
       .HasForeignKey(d => d.BaseUnitOfMeasureId)
       .OnDelete(DeleteBehavior.Restrict)
       .HasConstraintName("FK_Articles_BaseUnitOfMeasure");

Actually adding a collection of Articles to the unitOfMeasure class is enough and you no longer need to add builder.HasOne, unless you want a specific delete behavior.实际上,将文章集合添加到 unitOfMeasure class 就足够了,您不再需要添加 builder.HasOne,除非您想要特定的删除行为。

By the way, C# 9.0 introduces record types, which are a reference type that provides synthesized methods to provide value semantics for equality.顺便说一句,C# 9.0 引入了记录类型,它是一种引用类型,它提供综合方法来提供相等的值语义。 Records are immutable by default.默认情况下,记录是不可变的。 So you no longer need to have a private setter in your class.因此,您不再需要在 class 中使用私有设置器。

Problem solved.问题解决了。 I had no key in the UnitOfMeasure class.我在 UnitOfMeasure class 中没有密钥。 After adding the Id everything worked as expected.添加 ID 后,一切都按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM