代码之家  ›  专栏  ›  技术社区  ›  Andres

EF Core 2.1.4 Include正在对可选关系执行内部联接

  •  0
  • Andres  · 技术社区  · 7 年前

    我有一些麻烦包括一个实体相关的实体 Include 方法。

    var array = this.DbContext.Set<CustomerBranchOffice>()
    .Where(x => 1 == 1) //JUST FOR THE EXAMPLE
    .Include(x => x.CustomerWebSiteBrand)
    .ToArray();
    

    相关实体 CustomerWebSiteBrand 在里面 CustomerBranchOffice 如果在 CustomerSiteBrand 桌子。但是 方法总是执行 INNER JOIN LEFT JOIN . 这是在过滤我所有的 客户牧场办公室 s行与 内部连接

    SQL server表如下:

        CREATE TABLE dbo.Customers
        (
            Id INT NOT NULL PRIMARY KEY IDENTITY,
            Name VARCHAR(100) NOT NULL,
            WebSite VARCHAR(100) NULL
        )
    
        CREATE TABLE dbo.Brands
        (
            BrandCode VARCHAR(5) NOT NULL PRIMARY KEY,
            Name VARCHAR(100) NOT NULL
        )
    
        CREATE TABLE dbo.CustomerBranchOffices
        (
            Id INT NOT NULL PRIMARY KEY IDENTITY,
            IdCustomer INT NOT NULL,
            Location VARCHAR(100) NOT NULL,
            BrandCode  VARCHAR(5) NOT NULL
            CONSTRAINT FK_CustomerBranchOffices_Brands FOREIGN KEY (BrandCode) REFERENCES dbo.Brands(BrandCode),
            CONSTRAINT FK_CustomerBranchOffices_Brands FOREIGN KEY (CustomerId) REFERENCES dbo.Customers(Id)
        )
    
        CREATE TABLE dbo.CustomerWebSiteBrands
        (
            Id NOT NULL PRIMARY KEY IDENTITY,
            CustomerId INT NOT NULL,
            BrandCode VARCHAR(5) NOT NULL,
            BrandWebSiteUrl VARCHAR(100) NOT NULL
            UNIQUE (CustomerId, BrandCode),
            CONSTRAINT FK_CustomerWebSiteBrands_Brands FOREIGN KEY (BrandCode) REFERENCES dbo.Brands(BrandCode),
            CONSTRAINT FK_CustomerWebSiteBrands_Brands FOREIGN KEY (CustomerId) REFERENCES dbo.Customers(Id)
        )
    

    实体和映射如下:

    public class CustomerBranchOffice
    {
        public int Id { get; set; }
    
        public int IdCustomer { get; set; }
    
        public string Location { get; set; }
    
        public string BrandCode { get; set; }
    
        public CustomerWebSiteBrand CustomerWebSiteBrand { get; set; }
    }
    
    public CustomerBranchOfficeMap(EntityTypeBuilder<CustomerBranchOffice> entityBuilder)
    {
        entityBuilder.HasKey(x => x.Id);
    
        entityBuilder.Property(x => x.IdCustomer)
            .HasColumnName("IdCustomer")
            .HasColumnType("INTEGER")
            .IsRequired();
    
        entityBuilder.Property(x => x.Location)
            .HasColumnName("Location")
            .HasColumnType("VARCHAR(100)");
    
        entityBuilder.Property(x => x.BrandCode)
                .HasColumnName("BrandCode")
                .HasColumnType("VARCHAR(5)")
                .IsRequired();
    
        entityBuilder.HasOne(x => x.CustomerWebSiteBrand)
            .WithOne(x => x.CustomerBranchOffices)
            .HasForeignKey(x => new { x.IdCustomer, x.BrandCode } }) // The join is performed using these two properties (NOT NULLABLES)
            .HasPrincipalKey(x => new { x.IdCustomer, x.BrandCode });
    }
    
    public class CustomerWebSiteBrand
    {
        public int Id { get; set; }
    
        public int IdCustomer { get; set; }
    
        public string BrandCode { get; set; }
    
        public string BrandWebSiteUrl { get; set; }
    
        public ICollection<CustomerBranchOffice> CustomerBranchOffices { get; set; }
    }
    
    public CustomerWebSiteBrandMap(EntityTypeBuilder<CustomerWebSiteBrand> entityBuilder)
    {
        entityBuilder.HasKey(x => x.Id);
    
        entityBuilder.Property(x => x.IdCustomer)
            .HasColumnName("IdCustomer")
            .HasColumnType("INTEGER")
            .IsRequired();
    
        entityBuilder.Property(x => x.BrandCode)
                .HasColumnName("BrandCode")
                .HasColumnType("VARCHAR(5)")
                .IsRequired();
    
        entityBuilder.Property(x => x.BrandWebSiteUrl)
                .HasColumnName("BrandWebSiteUrl")
                .HasColumnType("VARCHAR(100)")
                .IsRequired();
    
        entityBuilder.HasMany(x => x.CustomerBranchOffices)
            .WithOne(x => x.CustomerWebSiteBran)
            .HasForeignKey(x => new { x.IdCustomer, x.BrandCode } }) // The join is performed using these two properties (NOT NULLABLES)
            .HasPrincipalKey(x => new { x.IdCustomer, x.BrandCode });
    }
    

    如您所见,使用属性执行关系 IdCustomer BranchCode

    我试着用 new { IdCustomer = (int?)x.IdCustomer, x.BrandCode } HasForeignKey HasPrincipalKey 配置,但是仍然,ORM执行 内部连接 左连接 包括 方法?

    0 回复  |  直到 7 年前