【问题标题】:How to map primary key to non-primary key in EF code first?如何首先将EF代码中的主键映射到非主键?
【发布时间】:2012-05-09 10:28:32
【问题描述】:

我有两张桌子

public class Table1
{
[Key]
int ID{get; set;}
Table2 table2{get; set;}
}
public class Table2
{
[Key]
int ID{get; set;}
int table1ID{get; set;}
//no instance of table i.e is relationship is uni-directional only
}


如何用 Table1.ID 和 Table2.table1ID 设置 table1 与 table2 的关系???

【问题讨论】:

    标签: asp.net asp.net-mvc-3 entity-framework linq-to-entities ef-code-first


    【解决方案1】:

    以此为例:

    public class Table1
    {
    [Key]
    int ID{get; set;}
    Table2 table2{get; set;}
    }
    public class Table2
    {
    [Key]
    int ID{get; set;}
    int table1ID{get; set;}
    
    [ForeignKey("table1ID")]
    Table1 table1{get; set;}
    //no instance of table i.e is relationship is uni-directional only
    }
    

    这意味着,Table2 有一个 Table1 引用 (table1),它的外键由 ForeignKeyAttribute 指定,将其设置为“table1ID”。

    【讨论】:

    • 这不是我需要的@RhymechaeT
    • 我一定是误会了。您想通过“table1ID”将这两个表关联起来吗?
    猜你喜欢
    • 2016-10-28
    • 2011-10-26
    • 2020-04-07
    • 2011-02-13
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多