Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架与数据注释的关系_Entity Framework_Foreign Keys - Fatal编程技术网

Entity framework 实体框架与数据注释的关系

Entity framework 实体框架与数据注释的关系,entity-framework,foreign-keys,Entity Framework,Foreign Keys,我正在努力使EF6和数据注释之间的关系正常工作 我有三(3)张桌子:主题、主题、主题 一个主题可以包含一个类型和一个节,因此我构建了如下表: Theme Id: int, autoincrement TypeId: int SectionId: int 那么 ThemeType Id: int, autoincrement 以及 对于我的代码第一类更新,我执行了以下操作: [Table("theme")] public class Theme: IDomain { [Autoincr

我正在努力使EF6和数据注释之间的关系正常工作

我有三(3)张桌子:主题、主题、主题

一个主题可以包含一个类型和一个节,因此我构建了如下表:

Theme
Id: int, autoincrement
TypeId: int
SectionId: int
那么

ThemeType
Id: int, autoincrement
以及

对于我的代码第一类更新,我执行了以下操作:

[Table("theme")]
public class Theme: IDomain
{
    [Autoincrement, Column("id"), Key]
    public int Id { get; set; }

    [Column("type_id")]
    public int TypeId { get; set; }

    [Column("section_id")]
    public int SectionId { get; set; }

    [ForeignKey("TypeId")]
    public virtual ThemeType Type { get; set; }

    [ForeignKey("SectionId")]
    public virtual ThemeSection Section { get; set; }
}

最后:

[Table("theme_section")]
public class ThemeSection: IDomain
{
    [Autoincrement, Column("id"), Key]
    public int Id { get; set; }

    [ForeignKey("Id")]
    public virtual Theme Theme { get; set; }
}

基于这里的链接,您需要在顶层定义ForeignKey,这就是主题,我是根据注释掉的代码这样做的,但它不起作用。继续得到一个关于多重性的错误?

我遵循了学习实体框架的示例,并在一定程度上使其工作,但仍然不知道我做错了什么。

我认为,它是由
自动递增属性
引起的,因为在这种情况下
Id
也是
FK
主题
,也就是说,它只能等于
Theme.Id
-而不是
Autoincrement
策略。什么是
[Autoincrement]
属性?我熟悉
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
,但不是那样。
[Table("theme_type")]
public class ThemeType: IDomain
{
    [Autoincrement, Column("id"), Key]
    public int Id { get; set; }

    [ForeignKey("Id")]
    public virtual Theme Theme { get; set; }
}
[Table("theme_section")]
public class ThemeSection: IDomain
{
    [Autoincrement, Column("id"), Key]
    public int Id { get; set; }

    [ForeignKey("Id")]
    public virtual Theme Theme { get; set; }
}