c# - 外键组件 'X' 不是类型 'Y' 的声明属性。验证它没有被明确排除

标签 c# entity-framework

这似乎是一个很常见的问题,但我已经尝试了一切,看不到任何问题。

这个问题好像是今天才开始的。完整错误信息:

“外键组件‘AllocationID’不是‘PortfolioSection’类型的声明属性。验证它没有被明确排除在模型之外,并且它是一个有效的原始属性。”

型号:

public class PortfolioSection 
{
    .... some stuff

    public int AllocationID { set; get; }

    [ForeignKey("AllocationID")]
    public virtual Allocation Allocation { get; set; }

    .... More stuff

}
public class Allocation 
{

    ... Some stuff



    [ForeignKey("PortfolioSections")]
    public int AllocationID { set; get; }


    public string Name { set; get;}


    public string Color { set; get; }


    public int SortOrder { set; get; }




    public virtual List<PortfolioSection> PortfolioSections { get; set; }


}

我没有做任何奇怪的配置,我可以看到会导致这个问题。难道Allocation的key是AllocationID而不仅仅是ID?

到目前为止,我已经尝试过: 1. 移除 PorfolioSection 到 Allocation 的导航属性。 2. 将 ForiegnKey 属性放在 AllocationID (ForeignKey("Allocation")) 而不是导航属性上。

最佳答案

您有一些放错地方的属性。如果你改变:

[ForeignKey("PortfolioSections")]
public int AllocationID { set; get; }

收件人:

[Key]
public int AllocationID { set; get; }

那么这应该可以正常工作。问题是您将键(AllocationID 是我假设的主键)声明为 PortfolioSections 表的外键。

关于c# - 外键组件 'X' 不是类型 'Y' 的声明属性。验证它没有被明确排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763732/

相关文章:

entity-framework - Entity Framework : mix table per type and table per hierarchy

entity-framework - 单个实体上的 EF 多对多

c# - 如何在本地/内部托管 Azure 辅助角色?

c# - 使用c#获取连接到我的热点的设备的MAC地址

c# - 比较 InnoDb 和 MyIsam MySql 表的相等性

c# - 查找重叠两个共线线段的线段的算法

c# - 斜率标准误差 c#

.net - 什么在 Entity Framework 中生成 SQL?

c# - 如何在 Lambda LINQ 表达式中创建 LEFT JOIN

c# - 如何在 linq to entities 中为几种类型编写通用 where?