iqtoolkit icon indicating copy to clipboard operation
iqtoolkit copied to clipboard

Problems with policies

Open Schurly opened this issue 5 years ago • 0 comments

Dear Matt,

great work! I’m currently evaluating IQToolkit to access MS Access databases. I have download and build the latest version successfully but run into the following issues: when I am using a session with policy to query details i get an exception in EntitySession.GetTable!

Here are some details: i have written this models:

[Table(Name = "Tests")]
public class Test : ViewModelBase
{
    [Column(IsPrimaryKey = true, IsGenerated = true)]
    public int TestId { get; set; }
    public string Description { get; set; }
    [Association(KeyMembers = nameof(TestId), RelatedKeyMembers = nameof(TestDetail.TestId))]
    public List<TestDetail> TestDetails { get; set; } = new List<TestDetail>();
}

[Table(Name = "TestDetails")]
public class TestDetail : ViewModelBase
{
    [Column(IsPrimaryKey = true, IsGenerated = true)]
    public int TestDetailId { get; set; }
    public int TestId { get; set; }
    public string TestDetailDescription { get; set; }
    [Association(Member = nameof(TestDetail.Test), KeyMembers = nameof(TestId), RelatedKeyMembers = "TestId")]
    public Test Test { get; set; }
}

Test case 1: Query Tests + TestDetails with policy "policy.IncludeWith<Test>(t => t.TestDetails)"

    static void TestProviderWithPolicy()
    {
        var policy = new EntityPolicy();
        policy.IncludeWith<Test>(t => t.TestDetails);
        var provider = new AccessQueryProvider("C:\\Users\\z271460\\Documents\\Tests.accdb").WithPolicy(policy);
        /// Query Tests + TestDetails
        /// -> I get all dependent TestDetail records but TestDetail.Test (parent) is null (not set)
        /// 
        var result = provider.GetTable<Test>().ToList();
    }

I get all dependent TestDetail records but TestDetail.Test (parent) is null (not set)

image

Test case 2: Query Tests + TestDetails with policies "policy.IncludeWith<Test>(t => t.TestDetails);" and "policy.IncludeWith<TestDetail>(t => t.Test);"

    static void TestProviderWithPolicy2()
    {
        var policy = new EntityPolicy();
        policy.IncludeWith<Test>(t => t.TestDetails);
        policy.IncludeWith<TestDetail>(t => t.Test);
        var provider = new AccessQueryProvider("C:\\Users\\z271460\\Documents\\Tests.accdb").WithPolicy(policy);
        /// Query Tests + TestDetails
        /// -> I get all dependent TestDetail records and TestDetail.Test (parent) is set.
        /// But TestDetail.Test.TestDetails is not set.
        /// So i think "recursions" wont work correctly and TestDetail.Test is a new instance of Test?
        /// 
        var result = provider.GetTable<Test>().ToList();
    }

I get all dependent TestDetail records and TestDetail.Test (parent) is set. But TestDetail.Test.TestDetails is not set. So i think "recursions" wont work correctly and TestDetail.Test is a new instance of Test?

image

Test case 3: Session without policies

    static void TestSessionWithoutPolicy()
    {
        var provider = new AccessQueryProvider("C:\\Users\\z271460\\Documents\\Tests.accdb");
        var session = new EntitySession(provider);
        /// i get all Tests without TestDetails as expected -> ok
        /// 
        var result = session.GetTable<Test>().ToList();
    }

Works as excpected!

Test case 4: Session with policy -> causes an exception

    static void TestSessionWithPolicy()
    {
        var policy = new EntityPolicy();
        policy.IncludeWith<Test>(t => t.TestDetails);
        var provider = new AccessQueryProvider("C:\\Users\\z271460\\Documents\\Tests.accdb").WithPolicy(policy);
        var session = new EntitySession(provider);
        /// i get an exception in EntitySession.GetTable
        /// 
        var result = session.GetTable<Test>().ToList();
    }

image

image

I think there are 2 problems causing this exception (?):

  1. parameter "entity" is not set (null) in EntitySession.OnEntityMaterialized -> which will cause the exception later in EntitySession.GetTable
  2. parameter "instance" is not an object but an KeyValuePair<int, object> in EntitySession.OnEntityMaterialized

I have tried many workarounds to avoid this exception and get sessions with policies working! But without success :-( ... Could you please help?

Schurly avatar Sep 30 '20 06:09 Schurly