0

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.

Community
  • 1
  • 1
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • 1
    Can i suggest a better Repository pattern? – Masoud Andalibi Dec 30 '16 at 17:33
  • It would be helpful to share the "weird exception" instead of having everyone guess. – Jasen Dec 30 '16 at 17:45
  • 1
    You didn't share any details, but one thing that catches my eye is that you should use "Expression>" instead of "Func" for your "where" parameter in GetSingle. – Evk Dec 30 '16 at 18:18
  • @Valkyriee you may. Do you have one in mind or link to a tutorial on such a better pattern? This one seemed pretty efficient. If I could get it to work it would be just fine. – SoftwareSavant Dec 30 '16 at 20:06
  • @Jasen, please see the post I linked to for the weird exception thank you. – SoftwareSavant Dec 30 '16 at 20:07
  • @Evk I thought I shared quite a bit of details... Perhaps too much? But the issue is, when this pattern is not throwing an exception somewhere (again, please see the post I linked to earlier), it is returning empty values on queries that I know should be returning results. – SoftwareSavant Dec 30 '16 at 20:11
  • I have followed your link but there is no description of an exception there either. – strongbutgood Dec 30 '16 at 20:13
  • Hey you can check these 2 links: http://stackoverflow.com/questions/41319440/how-to-set-some-of-entity-properties-in-repository-in-c/41319590#41319590 and http://stackoverflow.com/questions/41349080/ef-core-how-add-primary-key/41349133#41349133 should give you some good idea – Masoud Andalibi Dec 30 '16 at 20:14
  • Just looked them over. Those are pretty clean. I don't think either of them resolves the current issue I am having now. So if I treat my DAO like a console application and have a main class and do simple queries, I can get results back for simple querries. I however want to separate my DAO from my Business logic, from my controller logic, from my View (JSON, HTML, XML, etc). The pattern that I am trying to emulate always return null values. Even though I know some values should be returned. – SoftwareSavant Dec 30 '16 at 20:23
  • @DmainEvent you have to combine both links together to get the best result. do you want me to make a sample for you? with DbContext Class and Migration and etc – Masoud Andalibi Dec 30 '16 at 20:39
  • A sample would be helpful. Again, I must have made a mistake somewhere. I am just not sure where. – SoftwareSavant Dec 30 '16 at 20:42

0 Answers0