我有一个POCO类BankAccount
这是public
和所有的成员都public properties
和导航属性设置为virtual
。这是什么EF6行为查找原因()不返回一个代理,但单()不会返回代理
实体框架6.1.2使用Find()
方法从数据库正确加载它作为POCO。但是,据我所知,它应该返回一个Proxy类实例而不是POCO实例!实际上,当我使用Single()
,SingleOrDefault()
,First()
或FirstOrDefault()
代理类实例正确返回。
这是怎么回事,这是预期的行为,如果不是什么会导致这种情况发生?
下面是POCO类:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
namespace AbcBankModels
{
//[DataContract]
[Table("BankAccount")]
public class BankAccount
{
[Key]
[Column(Order = 1)]
//[DataMember]
//[Range(0, 9999999)]
//[StringLength(7, MinimumLength = 7)]
//[Display(Name = "Account Number")]
public virtual string BankAccountId { get; set; }
[Key]
[Column(Order = 2)]
//[DataMember]
//[Range(0, 999999)]
//[StringLength(6, MinimumLength = 6)]
//[Display(Name = "Sort Code")]
public virtual string BankBranchId { get; set; }
//[DataMember]
public virtual BankBranch BankBranch { get; set; }
//[DataMember]
//[ForeignKey("ApplicationUser")]
public virtual string ApplicationUserId { get; set; }
//[DataMember]
public virtual User ApplicationUser { get; set; }
//[DataMember]
public virtual ICollection<BankCard> BankCardList { get; set; }
//[DataMember]
public virtual ICollection<BankTransaction> BankTransactionList { get; set; }
//[DataMember]
//[Display(Name = "Account Status")]
//[EnumDataType(typeof(EnumAccountStatus))]
public virtual EnumAccountStatus AccountStatus { get; set; }
//[DataMember]
//[Display(Name = "Account Type")]
//[EnumDataType(typeof(EnumBankAccountType))]
public virtual EnumBankAccountType AccountType { get; set; }
//[DataMember]
//[DataType(DataType.DateTime)]
//[Display(Name = "Date Account Opened")]
public virtual DateTime? AccountCreationDateTime { get; set; }
//[DataMember]
//[DataType(DataType.DateTime)]
//[Display(Name = "Date Account Closed")]
public virtual DateTime? AccountClosureDateTime { get; set; }
//[DataMember]
//[DataType(DataType.Currency)]
//[Display(Name = "Account Overdraft Limit")]
public virtual decimal AccountOverdraft { get; set; }
//[DataMember]
//[Display(Name = "Account Overdraft Interest Rate")]
public virtual decimal AccountOverdraftInterestRate { get; set; }
//[DataMember]
//[Display(Name = "Account Overdraft Usage Monthly Fee")]
public virtual decimal AccountOverdraftFacilityMonthlyCost { get; set; }
//[DataMember]
//[Display(Name = "Account Monthly Fee")]
public virtual decimal AccountMonthlyCost { get; set; }
//[DataMember]
//[Display(Name = "Account Interest Rate")]
public virtual decimal AccountInterestRate { get; set; }
}
}
这里是一个不会返回代理的方法:
public static BankAccount FindBankAccount(ApplicationDbContext applicationDbContext, string bankAccountId, string bankBranchId, string userId)
{
if (String.IsNullOrWhiteSpace(bankAccountId) || String.IsNullOrWhiteSpace(bankBranchId)) return null;
var bankAccount = applicationDbContext.BankAccountList.SingleOrDefault(a => a.BankAccountId == bankAccountId && a.BankBranchId == bankBranchId);
if (bankAccount == null) return null;
if (string.IsNullOrWhiteSpace(userId)) return bankAccount;
if (bankAccount.ApplicationUserId != userId) return null;
return bankAccount;
}
这里是一个不返回代理的方法:
public static BankAccount FindBankAccount(ApplicationDbContext applicationDbContext, string bankAccountId,
string bankBranchId, string userId)
{
if (String.IsNullOrWhiteSpace(bankAccountId) || String.IsNullOrWhiteSpace(bankBranchId)) return null;
var bankAccount = applicationDbContext.BankAccountList.Find(bankAccountId, bankBranchId);
if (bankAccount == null) return null;
if (string.IsNullOrWhiteSpace(userId)) return bankAccount;
if (bankAccount.ApplicationUserId != userId) return null;
return bankAccount;
}