简体   繁体   English

未找到指向 Model 的 EF Core 导航。 在配置之前将导航添加到实体类型

[英]EF Core Navigation to a Model not found. Add navigation to entity type before configuring it

I have two models我有两个模型

public  class PickTaskListAssignment
    {
        [Key]
        public System.Guid PickTaskListAssignmentID { get; set; }
        public Guid PickTaskListAssignmentStatusID { get; set; }
        
        public virtual PickTaskListAssignmentStatus PickTaskListAssignmentStatus { get; set; }
    }

    public class PickTaskListAssignmentStatus
    {
       

        [Key]
        public Guid PickTaskListAssignmentStatusID { get; set; }
        public string Description { get; set; }

        public virtual ICollection<PickTaskListAssignment> PickTaskListAssignment { get; set; }
    }

Both have been added on the db context.两者都已添加到数据库上下文中。

public virtual DbSet<PickTaskListAssignmentStatus> PickTaskListAssignmentStatus { get; set; }
public virtual DbSet<PickTaskListAssignment> PickTaskListAssignment { get; set; }

But everytime I type in do.net ef migrations add picklist但每次我输入do.net ef migrations add picklist

I encounter this error我遇到这个错误

Navigation '.Models.PickPack.PickTaskListAssignmentStatus (Dictionary<string, object>).PickTaskListAssignment' was not found.找不到导航“.Models.PickPack.PickTaskListAssignmentStatus (Dictionary<string, object>).PickTaskListAssignment”。 Please add the navigation to the entity type before configuring it.请在配置之前将导航添加到实体类型。

Any ideas?有任何想法吗?

Like the comment said, for automatic navigation configuration, naming has to match.正如评论所说,对于自动导航配置,命名必须匹配。

When in you do not want to depend on the automatically configured relation, you can also explicitly define the relation in the Database Context.当您不想依赖自动配置的关系时,您也可以在数据库上下文中显式定义关系。 For example, in the OnModelCreating you can specify the following:例如,在OnModelCreating中,您可以指定以下内容:

 modelBuilder.Entity<PickTaskListAssignment>(entity =>
    {
          
        entity.HasOne(d => d.PickTaskListAssignmentStatus)
            .WithMany(p => p.PickTaskListAssignment)
            .HasForeignKey(d => d.PickTaskListAssignmentStatusID);
     });

For more reference, see EF Core Documentation有关更多参考,请参阅EF Core 文档

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

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