EF:导航属性不是类型的声明属性

时间:2016-03-16 22:13:19

标签: c# entity-framework

我很不知道为什么这不起作用,它似乎只在上周工作,我没有看到这些类的任何变化。有人可以告诉我为什么会收到错误:

  

"导航属性' Node'不是类型的声明属性   ' NWatchRelation&#39 ;.确认未明确排除   模型,它是一个有效的导航属性。"

NWatchRelation实体

public class NWatchRelation : INWatchRelation
    {
        private NWatchRelation()
        {
        }

        public NWatchRelation(int nodeId, int relatedNodeId)
        {
            NodeId = nodeId;
            RelatedNodeId = relatedNodeId;
        }

        public NWatchRelation(NWatchNode node, NWatchNode relatedNode)
        {
            Node = node;
            RelatedNode = relatedNode;
        }

        public int Id { get; set; }

        /// <summary>
        /// Foreign Key for Node
        /// </summary>
        public int NodeId { get; set; }

        /// <summary>
        /// Node
        /// </summary>
        public NWatchNode Node { get; set; }

        /// <summary>
        /// Foreign Key for RelatedNode
        /// </summary>
        public int RelatedNodeId { get; set; }

        /// <summary>
        /// Related Node
        /// </summary>
        public NWatchNode RelatedNode { get; set; }

        /// <summary>
        /// Relationship Type
        /// </summary>
        public NWatch.NWatchRelationType RelationType { get; set; }

        INWatchNode INWatchRelation.Node
        {
            get
            {
                return Node;
            }
        }

        INWatchNode INWatchRelation.RelatedNode
        {
            get
            {
                return RelatedNode;
            }
        }
    }

配置

// NWatchRelation
            modelBuilder.Entity<NWatchRelation>().Map(m =>
            {
                m.ToTable("NWatchRelations");
            });
            modelBuilder.Entity<NWatchRelation>()
                .HasKey(t => t.Id)
                .Property(t => t.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<NWatchRelation>().HasRequired(t => t.Node).
                WithMany().HasForeignKey(t => t.NodeId).WillCascadeOnDelete(false);
            modelBuilder.Entity<NWatchRelation>().HasRequired(t => t.RelatedNode).
                WithMany().HasForeignKey(t => t.RelatedNodeId).WillCascadeOnDelete(false);

1 个答案:

答案 0 :(得分:2)

我认为如果将其视为导航属性,则必须指定外键属性

 [ForeignKey("Node"), Column(Order = 0)] 
 public int NodeId { get; set; }