I have been having huge issues getting Entity Framework (code-first, already existing Oracle DB, didn't use an edmx file to generate the classes, did it by hand) to work using a repository pattern.
For example:
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;
}
In the above function, the context is set, when I debug it, I can see all of the context objects. The Connection is set, I can step through and I can see the repository connection.
THe above method is implemented by a business layer method (separate project)
public CaptureUser GetUserByName(string firstName, string LastName) {
return _captureUserRepository.GetSingle(
cu => cu.firstName.Equals(firstName) &&
cu.lastName.Equals(LastName)
);
}
The above is the entity framework query that I am sending in to the IQueryable object that is applied. For this particular query I don't want to Eagerly load anything so I am not sending in any navigation properties (these would be applied to the dbQuery.Include(navigationProperty) foreach statement). The entity I am trying to retrieve:
[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; }
}
My object that extends 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; }
...
}
So I have issues when I attempt to retrieve any values. The returns result either come back as null, or they through some weird exception (please see this link My post on another exception I get). In any case, I also tried doing a simpler DAO as well, that also gave me problems.