实体框架如何保存自动增量ID

时间:2014-11-26 02:00:07

标签: entity-framework entity-framework-6

我有点厌倦EF如何使用自动增量主键保存相关表,这些表是其他表的外键,例如这两个相关的表:

public partial class LoanType
{
    public LoanType()
    {
        this.Loans = new HashSet<Loan>();
    }

    public int loan_type_id { get; set; }
    public string loan_type_name { get; set; }

    public virtual ICollection<Loan> Loans { get; set; }
}

public partial class Loan
{
    public Loan()
    {
        this.Collections = new HashSet<Collection>();
        this.Transactions = new HashSet<Transaction>();
    }

    public int loan_id { get; set; }
    public int loan_type_id { get; set; }
    public int emp_id { get; set; }
    public int voucher_id { get; set; }
    public int term_id { get; set; }
    public int comaker_id { get; set; }
    public System.DateTime loan_date_added { get; set; }

    public virtual ICollection<Collection> Collections { get; set; }
    public virtual Comaker Comaker { get; set; }
    public virtual Employee Employee { get; set; }
    public virtual LoanType LoanType { get; set; }
    public virtual Term Term { get; set; }
    public virtual Voucher Voucher { get; set; }
    public virtual ICollection<Transaction> Transactions { get; set; }
}

它们通过贷款类型ID相关,这是一个自动增量字段。如果我生成两个新实体,例如

,EF会知道id是什么
LoanType LT = new LoanType();

//assign new values

Loan loan = new Loan();

//assign values for loan

context.SaveChanges();
EF会知道新贷款类型的ID是什么,并将其链接到新创建的贷款吗?

0 个答案:

没有答案