本文介绍了EF:导航属性不是类型上的声明属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对这为什么不起作用一无所知,似乎上周才起作用,而且我对这些课程没有任何改变。有人可以让我知道为什么会出现错误:
I am very clueless as to why this isn't working, it seems to work just last week, and I am not seeing any changes to these classes. Can someone please let me know why I get the error:
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);
推荐答案
我认为您必须指定外键属性,如果您将其视为导航属性
I think you have to specify the foreign key attribute if you consider it as navigation property
[ForeignKey("Node"), Column(Order = 0)]
public int NodeId { get; set; }
这篇关于EF:导航属性不是类型上的声明属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!