{"action":"created","issue":{"url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/85","repository_url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters","labels_url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/85/labels{/name}","comments_url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/85/comments","events_url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/85/events","html_url":"https://github.com/jcachat/EntityFramework.DynamicFilters/issues/85","id":191935407,"number":85,"title":"Some bug about Guid,Guid? filter and Include query ","user":{"login":"lewyue","id":10785049,"avatar_url":"https://avatars.githubusercontent.com/u/10785049?v=3","gravatar_id":"","url":"https://api.github.com/users/lewyue","html_url":"https://github.com/lewyue","followers_url":"https://api.github.com/users/lewyue/followers","following_url":"https://api.github.com/users/lewyue/following{/other_user}","gists_url":"https://api.github.com/users/lewyue/gists{/gist_id}","starred_url":"https://api.github.com/users/lewyue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lewyue/subscriptions","organizations_url":"https://api.github.com/users/lewyue/orgs","repos_url":"https://api.github.com/users/lewyue/repos","events_url":"https://api.github.com/users/lewyue/events{/privacy}","received_events_url":"https://api.github.com/users/lewyue/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-11-28T07:21:40Z","updated_at":"2016-12-01T15:12:16Z","closed_at":null,"body":"And see the test code\r\n```\r\n    public interface ISoftDelete\r\n    {\r\n        bool IsDeleted { get; set; }\r\n    }\r\n```\r\n\r\n```\r\n    public interface IMayHaveTenant\r\n    {\r\n        Guid? TenantId { get; set; }\r\n    }\r\n```\r\n\r\n```\r\n    public class ProductType : ISoftDelete, IMayHaveTenant\r\n    {\r\n        public ProductType()\r\n        {\r\n        }\r\n\r\n        public int Id { get; set; }\r\n\r\n        public string Name { get; set; }\r\n\r\n        public int? ParentId { get; set; }\r\n\r\n        [ForeignKey(\"ParentId\")]\r\n        public ProductType Parent { get; set; }\r\n        [InverseProperty(\"Parent\")]\r\n        public ICollection<ProductType> Children { get; set; }\r\n\r\n        public bool IsDeleted { get; set; }\r\n\r\n        public Guid? TenantId { get; set; }\r\n\r\n    }\r\n```\r\n\r\n```\r\n      public class UserInfo : ISoftDelete\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public string Name { get; set; }\r\n\r\n        public int? ParentId { get; set; }\r\n\r\n        [ForeignKey(\"ParentId\")]\r\n        public UserInfo Parent { get; set; }\r\n        [InverseProperty(\"Parent\")]\r\n        public ICollection<UserInfo> Children { get; set; }\r\n\r\n        public bool IsDeleted { get; set; }\r\n    }\r\n```\r\n\r\n```\r\n modelBuilder.Filter(\"IsDeleted\", (ISoftDelete d) => d.IsDeleted, false);\r\nmodelBuilder.Filter(\"IMayHaveTenant\", (IMayHaveTenant t, Guid? tenantId) => t.TenantId == tenantId || (Guid?)t.TenantId == null, Guid.Empty);\r\n```\r\n\r\n```\r\n        public static void InitData()\r\n        {\r\n            ShopDbContext context = new ShopDbContext();\r\n\r\n            if (context.ProductTypes.Count() > 0) return;\r\n\r\n            ProductType productType1 = new ProductType()\r\n            {\r\n                Name = \"1\",\r\n                ParentId = 1,\r\n                IsDeleted = false,\r\n                TenantId = new Guid(\"ffffffff-ffff-ffff-ffff-000000000000\"),\r\n            };\r\n            ProductType productType2 = new ProductType()\r\n            {\r\n                Name = \"2\",\r\n                ParentId = 1,\r\n                IsDeleted = false,\r\n                TenantId = new Guid(\"ffffffff-ffff-ffff-ffff-000000000000\"),\r\n            };\r\n            ProductType productType3 = new ProductType()\r\n            {\r\n                Name = \"3\",\r\n                ParentId = 1,\r\n                IsDeleted = true,\r\n                TenantId = null,\r\n            };\r\n            ProductType productType4 = new ProductType()\r\n            {\r\n                Name = \"4\",\r\n                ParentId = 1,\r\n                IsDeleted = true,\r\n                TenantId = new Guid(\"ffffffff-ffff-ffff-ffff-000000000000\"),\r\n            };\r\n            ProductType productType5 = new ProductType()\r\n            {\r\n                Name = \"5\",\r\n                ParentId = 1,\r\n                IsDeleted = false,\r\n                TenantId = null,\r\n            };\r\n            context.ProductTypes.Add(productType1);\r\n            context.ProductTypes.Add(productType2);\r\n            context.ProductTypes.Add(productType3);\r\n            context.ProductTypes.Add(productType4);\r\n            context.ProductTypes.Add(productType5);\r\n            context.SaveChanges();\r\n\r\n            UserInfo userInfo1 = new UserInfo()\r\n            {\r\n                Name = \"1\",\r\n                ParentId = null,\r\n                IsDeleted = false\r\n            };\r\n            UserInfo userInfo2 = new UserInfo()\r\n            {\r\n                Name = \"2\",\r\n                ParentId = null,\r\n                IsDeleted = false\r\n            };\r\n            UserInfo userInfo3 = new UserInfo()\r\n            {\r\n                Name = \"3\",\r\n                ParentId = null,\r\n                IsDeleted = false\r\n            };\r\n            UserInfo userInfo4 = new UserInfo()\r\n            {\r\n                Name = \"4\",\r\n                ParentId = 1,\r\n                IsDeleted = false\r\n            };\r\n            context.UserInfos.Add(userInfo1);\r\n            context.UserInfos.Add(userInfo2);\r\n            context.UserInfos.Add(userInfo3);\r\n            context.UserInfos.Add(userInfo4);\r\n            context.SaveChanges();\r\n        }\r\n```\r\n\r\n```\r\n           ShopDbContext context = new ShopDbContext();\r\n\r\n            //Userinfo have an ISoftDelete filter,and type is bool\r\n            //this will not load parent when EntityFramework.DynamicFilters >= 2.0.0,but load correct when <=1.4.11\r\n            //The correct SQL need \"[Project1].[ParentId] = [Extend2].[Id]\" but real SQL is \"[Project1].[Id] = [Extend2].[ParentId]\" \r\n            //primary and forign opposite\r\n            var userInfo4 = context.UserInfos\r\n                .Include(t => t.Parent)\r\n                .FirstOrDefault(t => t.Id == 4);\r\n            Console.WriteLine(string.Format(\"Refer loaded:{0}\", userInfo4.Parent != null));\r\n\r\n\r\n            //Userinfo have an ISoftDelete filter type is bool and an IMayHaveTenant filter type is Guid?\r\n            //When IMayHaveTenand have an not null value,it get entity OK\r\n            var key = new Guid(\"ffffffff-ffff-ffff-ffff-000000000000\");\r\n            context.SetFilterGlobalParameterValue(\"IMayHaveTenant\", (Guid?)key);\r\n            var type2 = context.ProductTypes\r\n                .FirstOrDefault(t => t.Id == 2);\r\n            Console.WriteLine(string.Format(\"Entity loaded:{0}\", type2 != null));\r\n\r\n            //And now load parent,thie parent refer is not loaded\r\n            type2 = context.ProductTypes\r\n                .Include(t => t.Parent)\r\n                .FirstOrDefault(t => t.Id == 2);\r\n            Console.WriteLine(string.Format(\"Refer loaded:{0}\", type2.Parent != null));\r\n\r\n            //When IMayHaveTenand is null,then entity will not load\r\n            //Current SQL is  (TenantId IS NULL and @TenantId IS NULL) OR ([TenantId]=@TenantId) \r\n            //But all version SQL will like  [Extent1].[TenantId] = (CAST(NULL AS uniqueidentifier)) ,CAST( [Extent1].[TenantId] AS uniqueidentifier) IS NULL),[Extent1].[TenantId]=NULL and so on \r\n            context.SetFilterGlobalParameterValue(\"IMayHaveTenant\", \"TenantId\", (Guid?)null);\r\n            var type3 = context.ProductTypes\r\n                .FirstOrDefault(t => t.Id == 3);\r\n            Console.WriteLine(string.Format(\"Entity loaded:{0}\", type3 != null));\r\n\r\n            Console.ReadKey();\r\n            return;\r\n```\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/comments/264198294","html_url":"https://github.com/jcachat/EntityFramework.DynamicFilters/issues/85#issuecomment-264198294","issue_url":"https://api.github.com/repos/jcachat/EntityFramework.DynamicFilters/issues/85","id":264198294,"user":{"login":"lewyue","id":10785049,"avatar_url":"https://avatars.githubusercontent.com/u/10785049?v=3","gravatar_id":"","url":"https://api.github.com/users/lewyue","html_url":"https://github.com/lewyue","followers_url":"https://api.github.com/users/lewyue/followers","following_url":"https://api.github.com/users/lewyue/following{/other_user}","gists_url":"https://api.github.com/users/lewyue/gists{/gist_id}","starred_url":"https://api.github.com/users/lewyue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lewyue/subscriptions","organizations_url":"https://api.github.com/users/lewyue/orgs","repos_url":"https://api.github.com/users/lewyue/repos","events_url":"https://api.github.com/users/lewyue/events{/privacy}","received_events_url":"https://api.github.com/users/lewyue/received_events","type":"User","site_admin":false},"created_at":"2016-12-01T15:12:16Z","updated_at":"2016-12-01T15:12:16Z","body":"OK, Now i explain Q2\r\nAny entity if have an filter value is Guid?Linq query will not get entity\r\n\r\n```\r\ncontext.ProductTypes.Get(1)\r\ncontext.ProductTypes.FirstOrDefault();\r\ncontext.ProductTypes.Where();\r\n```\r\n\r\nthe sql will have sql like\r\n\r\n` [Extent1].[TenantId]=NULL`\r\n\r\nFilter set is \r\nmodelBuilder.Filter(\"IMayHaveTenant\", (IMayHaveTenant t, Guid? tenantId) => t.TenantId == tenantId || (Guid?)t.TenantId == null, Guid.Empty);\r\nalso \r\nmodelBuilder.Filter(\"IMayHaveTenant\", (IMayHaveTenant t, Guid? tenantId) => t.TenantId == tenantId || !t.TenantId.HasValue(), Guid.Empty);\r\n\r\nthe sql should be \r\n\r\n` [Extent1].[TenantId] is NULL`\r\n\r\nSuch as my IMayHaveTenant filter,but if the filter value is int? long? the sql is correct like\r\n\r\n` [Extent1].[TenantId] is NULL`\r\n\r\nI have test in ef with no filter \r\ncontext.ProductTypes.Where(t.TenantId == null);\r\nit get an correct sql\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"lewyue"}}