Quantcast
Channel: Entity Framework
Viewing all 10318 articles
Browse latest View live

Commented Unassigned: Query: Parameters not cast to correct type causes performance issues [2531]

0
0
Running the following query:

```
string value = "ABC1234567";

var data = from c in ctx.Posts
where c.PostId == value
select c;
```
Produces the following T-SQL:
```
exec sp_executesql N'SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[PostTitle] AS [PostTitle],
[Extent1].[Body] AS [Body],
[Extent1].[Author] AS [Author]
FROM [dbo].[Post] AS [Extent1]
WHERE [Extent1].[PostId] = @p__linq__0',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'ABC1234567'
```
Which causes a clustered index scan instead of a seek. However, if the SQL is changed to :
```
exec sp_executesql N'SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[PostTitle] AS [PostTitle],
[Extent1].[Body] AS [Body],
[Extent1].[Author] AS [Author]
FROM [dbo].[Post] AS [Extent1]
WHERE [Extent1].[PostId] = @p__linq__0',N'@p__linq__0 char(10)',@p__linq__0=N'ABC1234567'
```
This will result in a clustered index seek.

Here is the table and index structure:
```
CREATE TABLE [dbo].[Post](
[PostId] [char](10) NOT NULL,
[PostTitle] [varchar](100) NOT NULL,
[Body] [varchar](255) NOT NULL,
[Author] [char](50) NOT NULL
) ON [PRIMARY]

/****** Object: Index [ClusteredIndex-20141001-082832] Script Date: 10/1/2014 8:58:38 AM ******/
CREATE UNIQUE CLUSTERED INDEX [ClusteredIndex-20141001-082832] ON [dbo].[Post]
(
[PostId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

```
A resolution to this would be greatly appreciated, we have a considerable amount of legacy data which uses char data types as primary keys.

Thanks!

Comments: How is PostId defined in your model, do you have MaxLength and IsUnicode= false defined?

Commented Unassigned: Query: Parameters not cast to correct type causes performance issues [2531]

0
0
Running the following query:

```
string value = "ABC1234567";

var data = from c in ctx.Posts
where c.PostId == value
select c;
```
Produces the following T-SQL:
```
exec sp_executesql N'SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[PostTitle] AS [PostTitle],
[Extent1].[Body] AS [Body],
[Extent1].[Author] AS [Author]
FROM [dbo].[Post] AS [Extent1]
WHERE [Extent1].[PostId] = @p__linq__0',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'ABC1234567'
```
Which causes a clustered index scan instead of a seek. However, if the SQL is changed to :
```
exec sp_executesql N'SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[PostTitle] AS [PostTitle],
[Extent1].[Body] AS [Body],
[Extent1].[Author] AS [Author]
FROM [dbo].[Post] AS [Extent1]
WHERE [Extent1].[PostId] = @p__linq__0',N'@p__linq__0 char(10)',@p__linq__0=N'ABC1234567'
```
This will result in a clustered index seek.

Here is the table and index structure:
```
CREATE TABLE [dbo].[Post](
[PostId] [char](10) NOT NULL,
[PostTitle] [varchar](100) NOT NULL,
[Body] [varchar](255) NOT NULL,
[Author] [char](50) NOT NULL
) ON [PRIMARY]

/****** Object: Index [ClusteredIndex-20141001-082832] Script Date: 10/1/2014 8:58:38 AM ******/
CREATE UNIQUE CLUSTERED INDEX [ClusteredIndex-20141001-082832] ON [dbo].[Post]
(
[PostId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

```
A resolution to this would be greatly appreciated, we have a considerable amount of legacy data which uses char data types as primary keys.

Thanks!

Comments: Thanks ErikEJ. I thought we had tried both but after talking with my team it appears that the IsUnicode(false) hadn't been tried. The MaxLength didn't have an affect but the SQL generated with IsUnicode(false) is using a Seek instead of a Scan. Again Thanks!

Edited Issue: UpForGrabs: Query: Cast introduced in query for Int16 parameters that may prevent SQL from using indexes [823]

0
0
This is a very similar issue to [186](http://entityframework.codeplex.com/workitem/186) and [187](http://entityframework.codeplex.com/workitem/187):

Model contains this class:

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public short Part { get; set; }
}

A query like this:
```
short param = 1;
var q = db.Products.Where(c => c.Part == param);

```
Gets a translation like this:
```
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Part] AS [Part],
[Extent1].[ReferralUrl] AS [ReferralUrl]
FROM [dbo].[Products] AS [Extent1]
WHERE CAST([Extent1].[Part] AS int) = @p__linq__0)

```
Although both the parameters and the column are of type Int16. As a consequence of this, there is now way a database server such as SQL Server can take advantage of an index over Products.Part.

Edited Issue: Query: Cast introduced in query for Nullable parameters that may prevent SQL from using indexes [187]

0
0
We got the attached repro and a report that when a query contains variables that are nullable<DateTime> we introduce an additional cast in the translated query that makes it impossible for SQL to take advantage of any index (similar to the perf issues with non-unicode strings we fixed for EF 4).

This item was migrated from the DevDiv work item tracking system [ID=392853].

Edited Issue: Query: Cast introduced for enum properties may prevent SQL from using indexes [186]

0
0
"In this it might have to do with the fact that it is using function mapping (it is a TVF), but I am not sure.

var products = from p in db.ProductsByName(""%ich%"")
where p.Category == Categories.Food
select p;

I think this kind of cast tends to prevent SQL from using indexes.

"

This item was migrated from the DevDiv work item tracking system [ID=371803].

Edited Feature: Support for WinRT (App Store/Metro) [403]

0
0
We really need the ability to use Entity Framework in metro applications. Hopefully Microsoft will release the SQL Server CE with Windows 8.

Closed Unassigned: Some issues are Fixed but in Future release [2526]

0
0
Some of these belong in 6.1.2 I think...

https://entityframework.codeplex.com/workitem/list/advanced?keyword=&status=All&type=All&priority=All&release=Future&assignedTo=All&component=All&sortField=AssignedTo&sortDirection=Ascending&page=0&reasonClosed=Fixed


Comments: Thanks Erik, moved them to the appropriate release.

Edited Issue: Migrations :: migrating from EF4.3.1 to 6.1.1 fails for SqlCE [2523]

0
0
Consider the following basic model:

```
public class Foo
{
public int Id { get; set; }
}

public class FooContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
}
```

Install the following packages:

```
Install-Package EntityFramework -Version 4.3.1
Install-Package EntityFramework.SqlServerCompact -Version 4.3.3
Install-Package SqlServerCompact -Version 4.0.8854.1
Install-Package Microsoft.SqlServer.Compact -Version 4.0.8854.2
```

Enable-Migrations
Add-Migration Mig1
Update-Database

now update to EF6.1.1:

```
Update-Package EntityFramework.SqlServerCompact
Update-Package Microsoft.SqlServer.Compact
```

Updatw-Database throws the following:

System.Data.SqlServerCe.SqlCeException (0x80004005): The foreign key constraint does not exist. [ PK_dbo.__MigrationHistory ]
at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.UpgradeHistory(IEnumerable`1 upgradeOperations)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.UpgradeHistory(IEnumerable`1 upgradeOperations)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The foreign key constraint does not exist. [ PK_dbo.__MigrationHistory ]

Commented Issue: Migrations :: migrating from EF4.3.1 to 6.1.1 fails for SqlCE [2523]

0
0
Consider the following basic model:

```
public class Foo
{
public int Id { get; set; }
}

public class FooContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
}
```

Install the following packages:

```
Install-Package EntityFramework -Version 4.3.1
Install-Package EntityFramework.SqlServerCompact -Version 4.3.3
Install-Package SqlServerCompact -Version 4.0.8854.1
Install-Package Microsoft.SqlServer.Compact -Version 4.0.8854.2
```

Enable-Migrations
Add-Migration Mig1
Update-Database

now update to EF6.1.1:

```
Update-Package EntityFramework.SqlServerCompact
Update-Package Microsoft.SqlServer.Compact
```

Updatw-Database throws the following:

System.Data.SqlServerCe.SqlCeException (0x80004005): The foreign key constraint does not exist. [ PK_dbo.__MigrationHistory ]
at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.UpgradeHistory(IEnumerable`1 upgradeOperations)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.UpgradeHistory(IEnumerable`1 upgradeOperations)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The foreign key constraint does not exist. [ PK_dbo.__MigrationHistory ]
Comments: Assigning to Arthur as he is processing the related pull request.

Edited Unassigned: Strange query generation behaviour when filtering / sorting over entities chain. [2521]

0
0
Guess we have the following classes structure:

```
public class ItemClass
{
[Key]
public Guid ItemClassId { set; get; }

[Required]
public string Code { set; get; }

public List<ItemCategory> ItemCategories { set; get; }
}

public class ItemCategory
{
[Key]
public Guid ItemCategoryId { set; get; }

[Required]
public string Code { set; get; }

[Required]
public Guid ItemClassId { set; get; }

public ItemClass ItemClass { set; get; }

public List<ItemType> ItemTypes { set; get; }
}

public class ItemType
{
[Key]
public Guid ItemTypeId { set; get; }

[Required]
public string Code { set; get; }

[Required]
public Guid ItemCategoryId { set; get; }

public ItemCategory ItemCategory { set; get; }
}

public class EfSortingTest : DbContext
{
public EfSortingTest()
{
}

public EfSortingTest(string connectionString)
: base(connectionString)
{
}

public DbSet<ItemClass> ItemClasses { get; set; }
public DbSet<ItemCategory> ItemCategories { get; set; }
public DbSet<ItemType> ItemTypes { get; set; }
}
```
When the following query is executed:

```
EfSortingTest context = new EfSortingTest();

var itemTypes = context.Set<ItemType>()
.Where
(
itemTypeSearch =>
itemTypeSearch.Code != "test" &&
itemTypeSearch.ItemCategory.ItemClass.Code != "test"
)
.OrderBy
(
itemTypeSearch =>
itemTypeSearch.ItemCategory.ItemClass.Code
)
.ToList();
```

it produces a little strange SQL query:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
LEFT OUTER JOIN [dbo].[ItemClasses] AS [Extent4] ON [Extent2].[ItemClassId] = [Extent4].[ItemClassId]
WHERE (N'test' <> [Extent1].[Code]) AND (N'test' <> [Extent3].[Code])
ORDER BY [Extent4].[Code] ASC
```

The ItemCategories table included to the query twice, first time as
```
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
```
and the second time as
```
LEFT OUTER JOIN [dbo].[ItemClasses] AS [Extent4] ON [Extent2].[ItemClassId] = [Extent4].[ItemClassId]
```

But if the filtering by the ItemTypes.Code (itemTypeSearch.Code != "test") is removed, the second LEFT OUTER JOIN is not generated.

I.e. the LINQ query

```
var itemTypes = context.Set<ItemType>()
.Where
(
itemTypeSearch =>
itemTypeSearch.Code != "test" &&
itemTypeSearch.ItemCategory.ItemClass.Code != "test"
)
.OrderBy
(
itemTypeSearch =>
itemTypeSearch.ItemCategory.ItemClass.Code
)
.ToList();
```

produces the following SQL query:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
WHERE N'test' <> [Extent3].[Code]
ORDER BY [Extent3].[Code] ASC
```

Why the filtering by the ItemTypes.Code produces another JOIN to the ItemClasses table?

Expected SQL query is:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
WHERE (N'test' <> [Extent1].[Code]) AND (N'test' <> [Extent3].[Code])
ORDER BY [Extent3].[Code] ASC
```

Commented Unassigned: Strange query generation behaviour when filtering / sorting over entities chain. [2521]

0
0
Guess we have the following classes structure:

```
public class ItemClass
{
[Key]
public Guid ItemClassId { set; get; }

[Required]
public string Code { set; get; }

public List<ItemCategory> ItemCategories { set; get; }
}

public class ItemCategory
{
[Key]
public Guid ItemCategoryId { set; get; }

[Required]
public string Code { set; get; }

[Required]
public Guid ItemClassId { set; get; }

public ItemClass ItemClass { set; get; }

public List<ItemType> ItemTypes { set; get; }
}

public class ItemType
{
[Key]
public Guid ItemTypeId { set; get; }

[Required]
public string Code { set; get; }

[Required]
public Guid ItemCategoryId { set; get; }

public ItemCategory ItemCategory { set; get; }
}

public class EfSortingTest : DbContext
{
public EfSortingTest()
{
}

public EfSortingTest(string connectionString)
: base(connectionString)
{
}

public DbSet<ItemClass> ItemClasses { get; set; }
public DbSet<ItemCategory> ItemCategories { get; set; }
public DbSet<ItemType> ItemTypes { get; set; }
}
```
When the following query is executed:

```
EfSortingTest context = new EfSortingTest();

var itemTypes = context.Set<ItemType>()
.Where
(
itemTypeSearch =>
itemTypeSearch.Code != "test" &&
itemTypeSearch.ItemCategory.ItemClass.Code != "test"
)
.OrderBy
(
itemTypeSearch =>
itemTypeSearch.ItemCategory.ItemClass.Code
)
.ToList();
```

it produces a little strange SQL query:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
LEFT OUTER JOIN [dbo].[ItemClasses] AS [Extent4] ON [Extent2].[ItemClassId] = [Extent4].[ItemClassId]
WHERE (N'test' <> [Extent1].[Code]) AND (N'test' <> [Extent3].[Code])
ORDER BY [Extent4].[Code] ASC
```

The ItemCategories table included to the query twice, first time as
```
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
```
and the second time as
```
LEFT OUTER JOIN [dbo].[ItemClasses] AS [Extent4] ON [Extent2].[ItemClassId] = [Extent4].[ItemClassId]
```

But if the filtering by the ItemTypes.Code (itemTypeSearch.Code != "test") is removed, the second LEFT OUTER JOIN is not generated.

I.e. the LINQ query

```
var itemTypes = context.Set<ItemType>()
.Where
(
itemTypeSearch =>
itemTypeSearch.Code != "test" &&
itemTypeSearch.ItemCategory.ItemClass.Code != "test"
)
.OrderBy
(
itemTypeSearch =>
itemTypeSearch.ItemCategory.ItemClass.Code
)
.ToList();
```

produces the following SQL query:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
WHERE N'test' <> [Extent3].[Code]
ORDER BY [Extent3].[Code] ASC
```

Why the filtering by the ItemTypes.Code produces another JOIN to the ItemClasses table?

Expected SQL query is:

```
SELECT
[Extent1].[ItemTypeId] AS [ItemTypeId],
[Extent1].[Code] AS [Code],
[Extent1].[ItemCategoryId] AS [ItemCategoryId]
FROM [dbo].[ItemTypes] AS [Extent1]
INNER JOIN [dbo].[ItemCategories] AS [Extent2] ON [Extent1].[ItemCategoryId] = [Extent2].[ItemCategoryId]
INNER JOIN [dbo].[ItemClasses] AS [Extent3] ON [Extent2].[ItemClassId] = [Extent3].[ItemClassId]
WHERE (N'test' <> [Extent1].[Code]) AND (N'test' <> [Extent3].[Code])
ORDER BY [Extent3].[Code] ASC
```
Comments: Assigning to Diego as he volunteered to investigate :)

Closed Unassigned: System.Data.Entity.Spatial.DbGeography.Area issue [2520]

0
0
There's a problem with DbGeography in EF6.0. The following WKT corresponds to a polygon of 48400 square meters area. The result is "510065621662560.0".

public void PolygonAreaMustBeAppr50000SqMeters()
{
string wkt = "POLYGON((-86.51669713 35.92556076, -86.51668506 35.92754423, -86.51424640 35.92753438, -86.51425853 35.92555092, -86.51669713 35.92556076))";
DbGeography polygon = DbGeography.FromText(wkt);

Assert.IsTrue(polygon.Area.Value < 100000);
}
Comments: __EF Team Triage:__ DbGeography is just a small wrapper over the top of the SQL Server (or other providers) spatial data type. This looks to be a SQL Server behavior as the following query also return a large number. ``` SELECT geometry::STGeomFromText('POLYGON((-86.51669713 35.92556076, -86.51668506 35.92754423, -86.51424640 35.92753438, -86.51425853 35.92555092, -86.51669713 35.92556076))', 0).STArea() ``` There is a SQL Server forum for asking SQL Server related questions - http://social.msdn.microsoft.com/Forums/sqlserver/en-US/home. Or you could open a Connect issue - http://connect.microsoft.com/SQLServer.

Commented Unassigned: The space 'SSpace' has no associated collection. [2524]

0
0
I am trying to get the entity model using model name. Here i got an exception when getting item using Database.SSpace.

TEST_Entities context = new TEST_Entities();
ObjectContext objContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)context).ObjectContext;
MetadataWorkspace workspace = objContext.MetadataWorkspace;

string ss = string.Format("TEST_Model.Store.{0}", "TEST_AWARD");
var categories = workspace.GetItems(DataSpace.SSpace);
var categories1 = workspace.GetItemCollection(DataSpace.SSpace);
var categoriesTable = workspace.GetItem<EntityType>(ss, DataSpace.SSpace);
Type ssss = categoriesTable.GetType();

Thanks in advance
Comments: __EF Team Triage:__ There isn't enough information here for us to be able to troubleshoot the issue. Could you provide the model that you are using (or even better a complete project that demonstrates the issue).

New Post: How to generate DML without returning SQL?

0
0
Fantastic! It will work. You guys made my day - I thought this is a dead end...

Thank you
Val

Closed Unassigned: Reverse Engineer Code First Ambiguities [2522]

0
0
This seems to work fairly well, with one exception: the generated mapping classes don't specify a fully qualified type as a type argument to EntityTypeConfiguration. So if you have any tables that generate the same class name as one of the mapping class names, then there's an ambiguity.

For instance, a legacy database I connected to had Product and ProductMap tables, the latter of which was a many-many mapping between products. Of course, EF6.1.1 generated a ProductMapMap mapping class for this many-many table, but the abbreviated entity type name, ProductMap, yielded a compile-time ambiguity with the mapping class name for Product.
Comments: __EF Team Triage:__ This would only be an issue in the Power Tools as the [new tooling we integrated into the EF Wizard in EF6.1](http://msdn.microsoft.com/en-us/data/jj200620) does not generate separate mapping classes. Because the Power Tools are now legacy and we are investing in the consolidated tooling we aren't planning to address this item.

Closed Unassigned: Runtime errors because vb partial class name is case sensitive [2516]

0
0
When using EF (6.1.1) in a vb library class with partial classes to extend the generated classes, you get a runtime error if the class names are not in the same case:

for example:

the generated class:

```
Partial Public Class TestClass
Public Property TestID As Integer
End Class

```
and the extended class:

```
Partial Public Class TESTClass
Public Property OtherField As String
End Class
```

Everything will compile correct, as in VB names are case insensitive. But at runtime you will get the error that EntModel.TestClass is not available.

Comments: __EF Team Triage:__ This isn't something we are going to address because making the type resolution case insensitive would break other languages. In VB.NET the fix is just to make sure the casing matches the generated class.

Commented Unassigned: System.Data.Entity.Spatial.DbGeography.Area issue [2520]

0
0
There's a problem with DbGeography in EF6.0. The following WKT corresponds to a polygon of 48400 square meters area. The result is "510065621662560.0".

public void PolygonAreaMustBeAppr50000SqMeters()
{
string wkt = "POLYGON((-86.51669713 35.92556076, -86.51668506 35.92754423, -86.51424640 35.92753438, -86.51425853 35.92555092, -86.51669713 35.92556076))";
DbGeography polygon = DbGeography.FromText(wkt);

Assert.IsTrue(polygon.Area.Value < 100000);
}
Comments: Thank you RoMiller. As it turns out that's the correct behavior. Unlike polygon which is defined on an infinite plane, where the clockwise-ness order of the points doesn't matter, it does matter for a polygon defined on a finite sphere. The way that I've had it specified actually defined polygon enclosing all of the earth except for a small 50000 sq m piece desired. Thank you for your time.

Created Unassigned: Performance Issues over 500+ entities model [2532]

0
0
Few days ago i've posted a topic related with the high cost on initial data model loading over 500+ tables. I've elaborated a little testing program to evidence this.

For the proof, the database is __AdventureWorks__ with 72 tables where the largest table with row count is [Sales].[SalesOrderDetail] (121,317 records):

1. On ___EF5___ without pre-generated views performing (select * from SalesOrderDetails where _condition_) the result is: __4.65__ seconds.

2. On ___EF5___ with pre-generated views performing (select * from SalesOrderDetails where _condition_) the result is: __4.30__ seconds.

3. Now, on ___EF6___ without pre-generated views performing the same query (select * from SalesOrderDetails where _condition_) the result is: __6.49__ seconds.

4. Finally, on ___EF6___ with pre-generated views performing the same query (select * from SalesOrderDetails where _condition_) the result is: __4.12__ seconds.

The attachment has two images with these results. The source code has been uploaded in my TFS online at [https://diegotrujillor.visualstudio.com/DefaultCollection/EntityFramework-PerformanceTest], please let me know the user(s) or email(s) to grant all access and proceed to download it to examine. The test was performed on a local server pointing to .\SQLEXPRESS.

So far, we can see some subtle differences and the picture doesn't look very daunting, however, the same scenario in a real prod environment with 538 tables definitely goes in a wrong direction. It is impossible to me to attach the original code and a database backup due to size and privacity stuff (i can send some pictures or even have a conference sharing my desktop to show running live). I've executed hundreds of queries attempting to compare the generated output trace on sql server profiler, and then paste and execute the sentence and the time consumed is 0.00 seconds in sql server editor.

On the live env, ___EF5___ without pre-generated views it can take up __259.8__ seconds in a table with 8049 records and 104 columns executing a very similar query respect to the mentioned above. This goes better with pre-generated views: __21.9__ seconds, one more time, the statement generated in sql server profiler takes 0.00 seconds to execute.

Nevertheless, on the live env, ___EF6___ it can take up __49.3__ seconds executing the same query without pre-generated views, with pre-generated: __47.9__ seconds. It looks like the pre-generated views doesn't cause any effect in ___EF6___ or it already have pre-generated views, from core functionality or something else, don't know.

Thus, i had to perform a downgrade to ___EF5___ as mentioned in my recent post [http://blogs.msdn.com/b/adonet/archive/2014/05/19/ef7-new-platforms-new-data-stores.aspx?CommentPosted=true#10561183].

I've already performed the same tests over database first and code first with same results. Actually i'm using the "Entity Framework Power Tools" add-in to pre-generate views, both projects, the real and testing are in .NET Framework 4.0, Visual Studio 2013 is the IDE and SQL Server 2008 R2 SP2 the DBMS.

Any help would be appreciated. Thanks in advance.

Edited Unassigned: Performance Issues over 500+ entities model [2532]

0
0
Few days ago i've posted a topic related with the high cost on initial data model loading over 500+ tables. I've elaborated a little testing program to evidence this.

For the proof, the database is __AdventureWorks__ with 72 tables where the largest table with row count is [Sales].[SalesOrderDetail] (121,317 records):

1. On ___EF5___ without pre-generated views performing (select * from SalesOrderDetails where _condition_) the result is: __4.65__ seconds.

2. On ___EF5___ with pre-generated views performing (select * from SalesOrderDetails where _condition_) the result is: __4.30__ seconds.

3. Now, on ___EF6___ without pre-generated views performing the same query (select * from SalesOrderDetails where _condition_) the result is: __6.49__ seconds.

4. Finally, on ___EF6___ with pre-generated views performing the same query (select * from SalesOrderDetails where _condition_) the result is: __4.12__ seconds.

The attachment has two images with these results. The source code has been uploaded in my TFS online at [https://diegotrujillor.visualstudio.com/DefaultCollection/EntityFramework-PerformanceTest ], please let me know the user(s) or email(s) to grant all access and proceed to download it to examine. The test was performed on a local server pointing to .\SQLEXPRESS.

So far, we can see some subtle differences and the picture doesn't look very daunting, however, the same scenario in a real prod environment with 538 tables definitely goes in a wrong direction. It is impossible to me to attach the original code and a database backup due to size and privacity stuff (i can send some pictures or even have a conference sharing my desktop to show running live). I've executed hundreds of queries attempting to compare the generated output trace on sql server profiler, and then paste and execute the sentence and the time consumed is 0.00 seconds in sql server editor.

On the live env, ___EF5___ without pre-generated views it can take up __259.8__ seconds in a table with 8049 records and 104 columns executing a very similar query respect to the mentioned above. This goes better with pre-generated views: __21.9__ seconds, one more time, the statement generated in sql server profiler takes 0.00 seconds to execute.

Nevertheless, on the live env, ___EF6___ it can take up __49.3__ seconds executing the same query without pre-generated views, with pre-generated: __47.9__ seconds. It looks like the pre-generated views doesn't cause any effect in ___EF6___ or it already have pre-generated views, from core functionality or something else, don't know.

Thus, i had to perform a downgrade to ___EF5___ as mentioned in my recent post [http://blogs.msdn.com/b/adonet/archive/2014/05/19/ef7-new-platforms-new-data-stores.aspx?CommentPosted=true#10561183 ].

I've already performed the same tests over database first and code first with same results. Actually i'm using the "Entity Framework Power Tools" add-in to pre-generate views, both projects, the real and testing are in .NET Framework 4.0, Visual Studio 2013 is the IDE and SQL Server 2008 R2 SP2 the DBMS.

Any help would be appreciated. Thanks in advance.

Closed Issue: Projections with .ToString() fails when UseDatabaseNullSemantics = true [2519]

0
0
In EF 6.1.2 beta:
ToString() fails for all datatypes when dbContext.Configuration.UseDatabaseNullSemantics = true
(with message "The specified cast from a materialized 'System.xxx' type to the 'System.String' type is not valid")

The generated sql with UseDatabaseNullSemantics = true:
SELECT [Extent1].[Id] AS [Id] FROM [dbo].[Entities] AS [Extent1]

The generated sql with UseDatabaseNullSemantics = false
SELECT [Extent1].[Id] AS [Id], CAST( [Extent1].[Id] AS nvarchar(max))AS [C1]FROM [dbo].[Entities] AS [Extent1]

```
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Db : DbContext
{
public DbSet<Entity> Entities { get; set; }
}

static void Main(string[] args)
{
var db = new Db();
db.Configuration.UseDatabaseNullSemantics = true;
db.Entities.Add(new Entity { Name = "Test" + DateTime.Now });
db.SaveChanges();

var list = db.Entities.Select(d => new { Id = d.Id.ToString() }).ToList();
//fails with The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.
}
```

Comments: verified, closing
Viewing all 10318 articles
Browse latest View live




Latest Images