Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasQueryFilter supporting any other type than string? #8630

Closed
ghost opened this issue May 28, 2017 · 2 comments
Closed

HasQueryFilter supporting any other type than string? #8630

ghost opened this issue May 28, 2017 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ghost
Copy link

ghost commented May 28, 2017

If I use the type int as query filter (HasQueryFilter) I get the below exception when trying to list entries that were added on table successfully. Anything i do wrong or is this a bug?

Exception message:
System.ArgumentException: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Void AddParameter(System.String, System.Object)'
Parameter name: arg1

below code doesn't work.

modelBuilder.Entity<Tenant>().Property<int>("TenantId").HasField("tenantId").Metadata.IsReadOnlyAfterSave = true;
modelBuilder.Entity<Tenant>().HasQueryFilter(b => EF.Property<int>(b, "TenantId") == tenantId);

modelBuilder.Entity<ServiceAccount>().Property<int>("TenantId").HasField("tenantId").Metadata.IsReadOnlyAfterSave = true;
modelBuilder.Entity<ServiceAccount>().HasQueryFilter(b => EF.Property<int>(b, "TenantId") == tenantId);

But with string type of TenantId then it works

modelBuilder.Entity<Tenant>().Property<string>("TenantId").HasField("tenantId").Metadata.IsReadOnlyAfterSave = true;
modelBuilder.Entity<Tenant>().HasQueryFilter(b => EF.Property<string>(b, "TenantId") == tenantId);

modelBuilder.Entity<ServiceAccount>().Property<string>("TenantId").HasField("tenantId").Metadata.IsReadOnlyAfterSave = true;
modelBuilder.Entity<ServiceAccount>().HasQueryFilter(b => EF.Property<string>(b, "TenantId") == tenantId);

Exception message:
System.ArgumentException: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Void AddParameter(System.String, System.Object)'
Parameter name: arg1

public override int SaveChanges()
{
   ChangeTracker.DetectChanges();

   foreach (var item in ChangeTracker.Entries().Where(e => e.State == EntityState.Added && e.Metadata.GetProperties().Any(p => p.Name == "TenantId")))
    {
        item.CurrentValues["TenantId"] = tenantId;
    }
    return base.SaveChanges();
}
 public void CanCreateServiceAccountForTenant()
        {
            var optionsBuilder = new DbContextOptionsBuilder<OnBoardingStore>();
            optionsBuilder.UseSqlServer(@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Test1;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

            using (var db = new OnBoardingStore(optionsBuilder.Options, "1"))
            {
                db.Database.EnsureCreated();
                db.Add(new Tenant() { DisplayName = "ABC" });
                db.Add(new ServiceAccount { DisplayName = "Service Account 1" });
                var saved = db.SaveChanges();
                Assert.IsTrue(saved == 2);
            }

            using (var db = new OnBoardingStore(optionsBuilder.Options, "2"))
            {
                db.Database.EnsureCreated();
                db.Add(new Tenant() { DisplayName = "ABC" });
                db.Add(new ServiceAccount { DisplayName = "Service Account 1" });
                var saved = db.SaveChanges();
                Assert.IsTrue(saved == 2);
            }

            using (var db = new OnBoardingStore(optionsBuilder.Options, "1"))
            {
                var tenants = db.Tenants.ToListAsync().GetAwaiter().GetResult();
                var serviceAccounts = db.ServiceAccounts.ToListAsync().GetAwaiter().GetResult();
                Assert.IsTrue(tenants.Count == 1);
                Assert.IsTrue(serviceAccounts.Count == 1);
            }
        }
@ajcvickers ajcvickers added this to the 2.0.0 milestone May 31, 2017
@anpete anpete modified the milestones: 2.0.0-preview2, 2.0.0 Jun 1, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0, 2.0.0-preview2 Jun 2, 2017
@ajcvickers
Copy link
Member

@anpete Moving this out of preview2 since it doesn't need to be done for that milestone. However, if you get to this while preview2 is still open, then please merge there.

@anpete
Copy link
Contributor

anpete commented Jun 2, 2017

Going to try 🤞

anpete added a commit that referenced this issue Jun 5, 2017
…n string?

- Adds a missing box operation.
anpete added a commit that referenced this issue Jun 6, 2017
…n string?

- Adds a missing box operation.
anpete added a commit that referenced this issue Jun 6, 2017
…n string?

- Adds a missing box operation.
@anpete anpete closed this as completed in 8929aec Jun 6, 2017
@anpete anpete added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 6, 2017
@anpete anpete modified the milestones: 2.0.0-preview2, 2.0.0 Jun 6, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview2, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants