首先使用EF代码的多个类

时间:2012-04-25 14:29:22

标签: asp.net-mvc-3 entity-framework-4 ef-code-first

调用member.TotalBalance

时出现无效的列错误

无效的列名称“Loan_ID”。

无效的列名称“Loan_ID1”。

继承我的片段

public class Member
{
    public string ID { get; set; }

    private ICollection<Loan> _Loans;
    public virtual ICollection<Loan> Loans
    {
        get { return _Loans ?? (_Loans = new HashSet<Loan>()); }
        set { _Loans = value; }
    }

    public virtual double TotalBalance
    {
        get
        {
            return Loans.Select(p => p.TotalBalance).Sum();
        }
    }
}

public class Loan
{
    public string ID { get; set; }

    public string MemberId { get; set; }

    private ICollection<LoanLedger> _LoanLedgers;
    public virtual ICollection<LoanLedger> LoanLedgers
    {
        get { return _LoanLedgers ?? (_LoanLedgers = new HashSet<LoanLedger>()); }
        set { _LoanLedgers = value; }
    }

    public virtual double TotalBalance
    {
        get
        {
            return LoanLedgers.Select(p => p.Amt).Sum();
        }
    }

}

public class LoanLedger
{
    public string ID { get; set; }
    public double Amt { get; set; }

    public string LoanId { get; set; }
}

以下是它在EF中的配置

//会员

HasKey(b => b.ID).Property(b => b.ID).HasColumnName("MBR_ID")
...

//贷款

HasKey(p => p.ID).HasRequired(a => a.Member).WithMany(i => i.Loans);
HasKey(p => p.ID).Property(p => p.ID).HasColumnName("LON_ID")
Property(p => p.MemberId).HasColumnName("MBR_ID");
...

// LoanLedger

HasKey(p => p.ID).HasRequired(a => a.Loan).WithMany(p => p.LoanLedgers);
HasKey(b => b.ID).Property(b => b.ID).HasColumnName("LON_LEDGER_ID")
Property(b => b.LoanId).HasColumnName("LON_ID");

...

提前致谢。

0 个答案:

没有答案