【问题标题】:EF: The navigation property is not a declared property on typeEF:导航属性不是类型上的声明属性
【发布时间】:2016-03-17 06:23:19
【问题描述】:

我不知道为什么这不起作用,它似乎在上周才起作用,而且我没有看到这些课程有任何变化。有人可以告诉我为什么会出现错误:

"导航属性“节点”不是类型上的声明属性 'NWatchRelation'。确认它没有被明确排除在 模型并且它是一个有效的导航属性。”

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);

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    如果你认为它是导航属性,我认为你必须指定外键属性

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

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      相关资源
      最近更新 更多