I use the Entity Framework to map two tables together, using Entity Splitting, as described here and here .
I found that if I executed .ToList() on an IQueryable<SplitEntity> , then the results would be obtained from Inner Join. However, if I take the same IQueryable and run .Count() , it will return the number of records returned by Full Join.
Here is the unit test that fails:
[TestMethod] public void GetCustomerListTest() {
It looks very bad. How can I get the .Count() method to return the results from Inner Join, like .ToList() ?
Update - SQL
I was mistaken about complete and internal associations.
.ToList () results in:
SELECT [Extent1].[CustomerNumber] AS [CustomerNumber], -- ...etc... [Extent2].[CustomerName] AS [CustomerName], -- ... etc... FROM [dbo].[CustomerTable1] AS [Extent1] INNER JOIN [dbo].[CustomerTable2] AS [Extent2] ON [Extent1].[CustomerNumber] = [Extent2].[CustomerNumber]
The result of .Count () results in:
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[customerTable2] AS [Extent1] ) AS [GroupBy1]
Update - DataContext code and object code
DataContext:
public class DataContext : DbContext { public DataContext() { Database.SetInitializer<DataContext>(null); } public DbSet<Customer> Customers { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new CustomerMapping()); } } }
Customer Mapping (FluentAPI):
public class CustomerMapping : EntityTypeConfiguration<Customer> { public CustomerMapping() { this.Map( m => { m.Properties( x => new { x.CustomerNumber, }); m.ToTable("CustomerTable1"); }) .Map( m => { m.Properties( x => new { x.CustomerName, }); m.ToTable("CustomerTable2"); }); } }
Client Object:
public class Customer { [Key] public string CustomerNumber { get; set; } public string CustomerName { get; set; } }