为什么实体框架存储库模式不起作用

时间:2016-12-30 17:27:17

标签: c# entity-framework linq

我一直在解决实体框架(代码优先,已经存在的Oracle数据库,没有使用edmx文件生成类,手动完成)以使用存储库模式工作时遇到了很大的问题。

例如:

public virtual T GetSingle(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties) {
        T item = null;
        using (var context = new CaptureEntitiesDbContext()) {
            IQueryable<T> dbQuery = context.Set<T>();

            //Apply eagerLoading for some reason
            foreach (Expression<Func<T, object>> navigationProperty in navigationProperties) {
                dbQuery = dbQuery.Include<T, object>(navigationProperty);
            }
                item = dbQuery
                       .AsNoTracking()
                       .FirstOrDefault(where);

        }
        return item;
    }

在上面的函数中,设置了上下文,当我调试它时,我可以看到所有的上下文对象。连接已设置,我可以单步执行,我可以看到存储库连接。

上述方法由业务层方法(单独项目)实现

public CaptureUser GetUserByName(string firstName, string LastName) {
        return _captureUserRepository.GetSingle(
            cu => cu.firstName.Equals(firstName) &&
                    cu.lastName.Equals(LastName)
                    );               
    }

以上是我发送给应用的IQueryable对象的实体框架查询。对于这个特定的查询,我不想急切地加载任何东西,所以我不发送任何导航属性(这些将应用于dbQuery.Include(navigationProperty)foreach语句)。我想要检索的实体:

    [Table("WACKKY_USER", Schema = "UL_DOC_WACKKY")]
public class WACKKYUser :BaseDTO
{
    [Key]
    [Column("WORKER_PARTY_ID")]
    public long workerPartyID { get; set; }
    [Column("FIRST_NM")]
    public string firstName { get; set; }
    [Column("LAST_NM")]
    public string lastName { get; set; }
    [Column("WACKKY_USER_STATUS_DC")]
    public string statusDescription { get; set; }
    [Column("RECORD_CREATE_GMTS")]
    public System.DateTime recordCreateGmTs {get;set;}
    [Column("USER_ID_EXPIRE_TS")]
    public System.DateTime userIdExpireTs { get; set; }
    [Column("RECORD_CREATE_USER_ID")]
    public string recordCreateUserID { get; set; }
    [Column("ESORT_DOC_VERIF_PCT")]
    public int esortDocVerifPct { get; set; }
    [Column("LOCATION_NM")]
    public string locationName { get; set; }
    [Column("COMPANY_NM")]
    public string companyNm { get; set; }

    public override System.Collections.Hashtable getDataMap() {
        Hashtable table = base.getDataMap();
        table.Add("workerPartyID", workerPartyID);
        table.Add("firstName", firstName);
        table.Add("lastName", lastName);
        table.Add("statusDescription", statusDescription);
        table.Add("recordCreateGmTs", recordCreateGmTs);
        table.Add("userIdExpireTs", userIdExpireTs);
        table.Add("recordCreateUserID", recordCreateUserID);
        table.Add("esortDocVerifPct", esortDocVerifPct);
        table.Add("companyNm", companyNm);
        table.Add("locationNm", locationName);
        return table;
    }

    [ForeignKey("userId")]
    public virtual System.Collections.Generic.ICollection<UserRoleRltn> userRoles { get; set; }
}

我的对象扩展了DbContext

public class CaptureEntitiesDbContext : DbContext
{
    public CaptureEntitiesDbContext() {
        GetInstance();
    }

    public CaptureEntitiesDbContext(string connectionString) : base(connectionString) {
        Database.SetInitializer<CaptureEntitiesDbContext>(null);
    }

    public static CaptureEntitiesDbContext GetInstance() {
        CaptureDBDAO.DBConnectionFactory connfact = new CaptureDBDAO.DBConnectionFactory();
        string connectionStr = connfact.GetConnection().ConnectionString;            
        CaptureEntitiesDbContext ctx = new CaptureEntitiesDbContext(connectionStr);
        //ctx.Database.Log = Console.Write; <-- Not sure what this is doing???
        return ctx;
    }

    ...
    public DbSet<CaptureUser> CaptureUsers { get; set; }
    ...
}

当我尝试检索任何值时,我遇到了问题。返回结果要么返回null,要么通过一些奇怪的异常(请参阅此链接My post on another exception I get)。 在任何情况下,我也尝试过做一个更简单的DAO,这也给了我一些问题。

0 个答案:

没有答案