EF6 lazy loading: loads entire collection when adding?
up vote
1
down vote
favorite
I have the following collection navigation property on my entity:
public virtual ICollection<OrderIntegrationLogEntry> LogEntries { get; set; }
And the OrderIntegrationLogEntry
entity has a little configuration:
this.HasKey(i => new {i.Id, i.IntegrationId});
this.HasRequired(i => i.Integration).WithMany(i => i.LogEntries).HasForeignKey(i => i.IntegrationId).WillCascadeOnDelete(true);
It seems that this line of code:
integration.LogEntries.Add(new OrderIntegrationLogEntry
{
Id = Guid.NewGuid(),
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
});
...results in a query which loads the contents of the collection:
SELECT [Extent1].[Id] AS [Id], [Extent1].[IntegrationId] AS [IntegrationId],
[Extent1].[CreatedUtc] AS [CreatedUtc], [Extent1].[Level] AS [Level],
[Extent1].[Message] AS [Message], [Extent1].[Detail] AS [Detail]
FROM [dbo].[OrderIntegrationLogEntries] AS [Extent1]
WHERE [Extent1].[IntegrationId] = @EntityKeyValue1
I wasn't expecting this: shouldn't it just stage an add? Do I need to configure in some other way?
entity-framework entity-framework-6
add a comment |
up vote
1
down vote
favorite
I have the following collection navigation property on my entity:
public virtual ICollection<OrderIntegrationLogEntry> LogEntries { get; set; }
And the OrderIntegrationLogEntry
entity has a little configuration:
this.HasKey(i => new {i.Id, i.IntegrationId});
this.HasRequired(i => i.Integration).WithMany(i => i.LogEntries).HasForeignKey(i => i.IntegrationId).WillCascadeOnDelete(true);
It seems that this line of code:
integration.LogEntries.Add(new OrderIntegrationLogEntry
{
Id = Guid.NewGuid(),
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
});
...results in a query which loads the contents of the collection:
SELECT [Extent1].[Id] AS [Id], [Extent1].[IntegrationId] AS [IntegrationId],
[Extent1].[CreatedUtc] AS [CreatedUtc], [Extent1].[Level] AS [Level],
[Extent1].[Message] AS [Message], [Extent1].[Detail] AS [Detail]
FROM [dbo].[OrderIntegrationLogEntries] AS [Extent1]
WHERE [Extent1].[IntegrationId] = @EntityKeyValue1
I wasn't expecting this: shouldn't it just stage an add? Do I need to configure in some other way?
entity-framework entity-framework-6
Lazy loading is triggered by a property getter. Andintegration.LogEntries.Add(…)
includes property getterintegration.LogEntries
.
– Ivan Stoev
15 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following collection navigation property on my entity:
public virtual ICollection<OrderIntegrationLogEntry> LogEntries { get; set; }
And the OrderIntegrationLogEntry
entity has a little configuration:
this.HasKey(i => new {i.Id, i.IntegrationId});
this.HasRequired(i => i.Integration).WithMany(i => i.LogEntries).HasForeignKey(i => i.IntegrationId).WillCascadeOnDelete(true);
It seems that this line of code:
integration.LogEntries.Add(new OrderIntegrationLogEntry
{
Id = Guid.NewGuid(),
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
});
...results in a query which loads the contents of the collection:
SELECT [Extent1].[Id] AS [Id], [Extent1].[IntegrationId] AS [IntegrationId],
[Extent1].[CreatedUtc] AS [CreatedUtc], [Extent1].[Level] AS [Level],
[Extent1].[Message] AS [Message], [Extent1].[Detail] AS [Detail]
FROM [dbo].[OrderIntegrationLogEntries] AS [Extent1]
WHERE [Extent1].[IntegrationId] = @EntityKeyValue1
I wasn't expecting this: shouldn't it just stage an add? Do I need to configure in some other way?
entity-framework entity-framework-6
I have the following collection navigation property on my entity:
public virtual ICollection<OrderIntegrationLogEntry> LogEntries { get; set; }
And the OrderIntegrationLogEntry
entity has a little configuration:
this.HasKey(i => new {i.Id, i.IntegrationId});
this.HasRequired(i => i.Integration).WithMany(i => i.LogEntries).HasForeignKey(i => i.IntegrationId).WillCascadeOnDelete(true);
It seems that this line of code:
integration.LogEntries.Add(new OrderIntegrationLogEntry
{
Id = Guid.NewGuid(),
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
});
...results in a query which loads the contents of the collection:
SELECT [Extent1].[Id] AS [Id], [Extent1].[IntegrationId] AS [IntegrationId],
[Extent1].[CreatedUtc] AS [CreatedUtc], [Extent1].[Level] AS [Level],
[Extent1].[Message] AS [Message], [Extent1].[Detail] AS [Detail]
FROM [dbo].[OrderIntegrationLogEntries] AS [Extent1]
WHERE [Extent1].[IntegrationId] = @EntityKeyValue1
I wasn't expecting this: shouldn't it just stage an add? Do I need to configure in some other way?
entity-framework entity-framework-6
entity-framework entity-framework-6
asked 17 hours ago
Kieren Johnstone
31.4k971118
31.4k971118
Lazy loading is triggered by a property getter. Andintegration.LogEntries.Add(…)
includes property getterintegration.LogEntries
.
– Ivan Stoev
15 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago
add a comment |
Lazy loading is triggered by a property getter. Andintegration.LogEntries.Add(…)
includes property getterintegration.LogEntries
.
– Ivan Stoev
15 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago
Lazy loading is triggered by a property getter. And
integration.LogEntries.Add(…)
includes property getter integration.LogEntries
.– Ivan Stoev
15 hours ago
Lazy loading is triggered by a property getter. And
integration.LogEntries.Add(…)
includes property getter integration.LogEntries
.– Ivan Stoev
15 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
As noted by Ivan you've called the LogEntries getter, which causes Lazy Loading.
If you don't want to turn off Lazy Loading, then instead of adding the log entity to the navigation property of the parent, just set the IntegrationId of the new entitiy and SaveChanges(). eg
var entry = new OrderIntegrationLogEntry()
{
Id = Guid.NewGuid(),
IntegrationId = integration.Id,
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
);
db.OrderIntegrationLogEntries.Add(entry);
db.SaveChanges();
Also if this is SQL Server (and probably other platforms too) use sequential GUID generation. Inserting a row with a random guid as its leading key column is needlessly expensive. For SQL Server you can generate sequential GUIDs in the database with the NEWSEQUENTIALID() function as a default, or on the client
public class SQLGuidUtil
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewSequentialId()
{
Guid guid;
UuidCreateSequential(out guid);
var s = guid.ToByteArray();
var t = new byte[16];
t[3] = s[0];
t[2] = s[1];
t[1] = s[2];
t[0] = s[3];
t[5] = s[4];
t[4] = s[5];
t[7] = s[6];
t[6] = s[7];
t[8] = s[8];
t[9] = s[9];
t[10] = s[10];
t[11] = s[11];
t[12] = s[12];
t[13] = s[13];
t[14] = s[14];
t[15] = s[15];
return new Guid(t);
}
}
How to Generate Sequential GUIDs for SQL Server in .NET
You should also consider flipping the order of your key columns if you expect the log entries to be accessed primarily on an Integration-by-Integration basis. That will store the log entries for an Integration together. EG
this.HasKey(i => new {i.IntegrationId, i.Id});
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
add a comment |
up vote
0
down vote
In order to enable lazy loading, EF creates proxy-classes derived from your model. In these classes it overrides the navigation properties to implement lazy loading. This is why your navigation properties should be defined virtual
so EF can override them.
When you call integration.LogEntries.Add
, the getter of your LogEntries
property is being called which triggers lazy loading operation.
You can temporarily disable lazy loading using the following code:
context.Configuration.LazyLoadingEnabled = false;
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As noted by Ivan you've called the LogEntries getter, which causes Lazy Loading.
If you don't want to turn off Lazy Loading, then instead of adding the log entity to the navigation property of the parent, just set the IntegrationId of the new entitiy and SaveChanges(). eg
var entry = new OrderIntegrationLogEntry()
{
Id = Guid.NewGuid(),
IntegrationId = integration.Id,
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
);
db.OrderIntegrationLogEntries.Add(entry);
db.SaveChanges();
Also if this is SQL Server (and probably other platforms too) use sequential GUID generation. Inserting a row with a random guid as its leading key column is needlessly expensive. For SQL Server you can generate sequential GUIDs in the database with the NEWSEQUENTIALID() function as a default, or on the client
public class SQLGuidUtil
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewSequentialId()
{
Guid guid;
UuidCreateSequential(out guid);
var s = guid.ToByteArray();
var t = new byte[16];
t[3] = s[0];
t[2] = s[1];
t[1] = s[2];
t[0] = s[3];
t[5] = s[4];
t[4] = s[5];
t[7] = s[6];
t[6] = s[7];
t[8] = s[8];
t[9] = s[9];
t[10] = s[10];
t[11] = s[11];
t[12] = s[12];
t[13] = s[13];
t[14] = s[14];
t[15] = s[15];
return new Guid(t);
}
}
How to Generate Sequential GUIDs for SQL Server in .NET
You should also consider flipping the order of your key columns if you expect the log entries to be accessed primarily on an Integration-by-Integration basis. That will store the log entries for an Integration together. EG
this.HasKey(i => new {i.IntegrationId, i.Id});
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
add a comment |
up vote
1
down vote
accepted
As noted by Ivan you've called the LogEntries getter, which causes Lazy Loading.
If you don't want to turn off Lazy Loading, then instead of adding the log entity to the navigation property of the parent, just set the IntegrationId of the new entitiy and SaveChanges(). eg
var entry = new OrderIntegrationLogEntry()
{
Id = Guid.NewGuid(),
IntegrationId = integration.Id,
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
);
db.OrderIntegrationLogEntries.Add(entry);
db.SaveChanges();
Also if this is SQL Server (and probably other platforms too) use sequential GUID generation. Inserting a row with a random guid as its leading key column is needlessly expensive. For SQL Server you can generate sequential GUIDs in the database with the NEWSEQUENTIALID() function as a default, or on the client
public class SQLGuidUtil
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewSequentialId()
{
Guid guid;
UuidCreateSequential(out guid);
var s = guid.ToByteArray();
var t = new byte[16];
t[3] = s[0];
t[2] = s[1];
t[1] = s[2];
t[0] = s[3];
t[5] = s[4];
t[4] = s[5];
t[7] = s[6];
t[6] = s[7];
t[8] = s[8];
t[9] = s[9];
t[10] = s[10];
t[11] = s[11];
t[12] = s[12];
t[13] = s[13];
t[14] = s[14];
t[15] = s[15];
return new Guid(t);
}
}
How to Generate Sequential GUIDs for SQL Server in .NET
You should also consider flipping the order of your key columns if you expect the log entries to be accessed primarily on an Integration-by-Integration basis. That will store the log entries for an Integration together. EG
this.HasKey(i => new {i.IntegrationId, i.Id});
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As noted by Ivan you've called the LogEntries getter, which causes Lazy Loading.
If you don't want to turn off Lazy Loading, then instead of adding the log entity to the navigation property of the parent, just set the IntegrationId of the new entitiy and SaveChanges(). eg
var entry = new OrderIntegrationLogEntry()
{
Id = Guid.NewGuid(),
IntegrationId = integration.Id,
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
);
db.OrderIntegrationLogEntries.Add(entry);
db.SaveChanges();
Also if this is SQL Server (and probably other platforms too) use sequential GUID generation. Inserting a row with a random guid as its leading key column is needlessly expensive. For SQL Server you can generate sequential GUIDs in the database with the NEWSEQUENTIALID() function as a default, or on the client
public class SQLGuidUtil
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewSequentialId()
{
Guid guid;
UuidCreateSequential(out guid);
var s = guid.ToByteArray();
var t = new byte[16];
t[3] = s[0];
t[2] = s[1];
t[1] = s[2];
t[0] = s[3];
t[5] = s[4];
t[4] = s[5];
t[7] = s[6];
t[6] = s[7];
t[8] = s[8];
t[9] = s[9];
t[10] = s[10];
t[11] = s[11];
t[12] = s[12];
t[13] = s[13];
t[14] = s[14];
t[15] = s[15];
return new Guid(t);
}
}
How to Generate Sequential GUIDs for SQL Server in .NET
You should also consider flipping the order of your key columns if you expect the log entries to be accessed primarily on an Integration-by-Integration basis. That will store the log entries for an Integration together. EG
this.HasKey(i => new {i.IntegrationId, i.Id});
As noted by Ivan you've called the LogEntries getter, which causes Lazy Loading.
If you don't want to turn off Lazy Loading, then instead of adding the log entity to the navigation property of the parent, just set the IntegrationId of the new entitiy and SaveChanges(). eg
var entry = new OrderIntegrationLogEntry()
{
Id = Guid.NewGuid(),
IntegrationId = integration.Id,
CreatedUtc = DateTime.UtcNow,
Message = message,
Level = level,
Detail = detail
);
db.OrderIntegrationLogEntries.Add(entry);
db.SaveChanges();
Also if this is SQL Server (and probably other platforms too) use sequential GUID generation. Inserting a row with a random guid as its leading key column is needlessly expensive. For SQL Server you can generate sequential GUIDs in the database with the NEWSEQUENTIALID() function as a default, or on the client
public class SQLGuidUtil
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewSequentialId()
{
Guid guid;
UuidCreateSequential(out guid);
var s = guid.ToByteArray();
var t = new byte[16];
t[3] = s[0];
t[2] = s[1];
t[1] = s[2];
t[0] = s[3];
t[5] = s[4];
t[4] = s[5];
t[7] = s[6];
t[6] = s[7];
t[8] = s[8];
t[9] = s[9];
t[10] = s[10];
t[11] = s[11];
t[12] = s[12];
t[13] = s[13];
t[14] = s[14];
t[15] = s[15];
return new Guid(t);
}
}
How to Generate Sequential GUIDs for SQL Server in .NET
You should also consider flipping the order of your key columns if you expect the log entries to be accessed primarily on an Integration-by-Integration basis. That will store the log entries for an Integration together. EG
this.HasKey(i => new {i.IntegrationId, i.Id});
answered 12 hours ago
David Browne - Microsoft
12.8k1524
12.8k1524
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
add a comment |
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
Thanks - I was using lazy loading to effectively hide the dependency on the context from the class which is using the parent entity. I'll have to expose it, or the add functionality, in some way to work around this, it seems. The tip with the composite keys is a great one, thanks. The sequential GUIDs - is there a .NET Core version that you know of? Also to quote MSDN there, "you should never use this UUID to identify an object that is not strictly local to your computer." I'm looking for a decent strategy moving forward - in this case, we should really be using regular int identity..
– Kieren Johnstone
9 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
You can use client-side sequential GUID generation from .NET Core, but only on Windows. SQL Server has a Linux implementation of UUIDCreateSequential, but AFAIK there's no other. The warning on MSDN for sequential GUIDs is for machines without unique MAC addresses. Even then it's unlikely that you would actually get a duplicate value generated.
– David Browne - Microsoft
8 hours ago
add a comment |
up vote
0
down vote
In order to enable lazy loading, EF creates proxy-classes derived from your model. In these classes it overrides the navigation properties to implement lazy loading. This is why your navigation properties should be defined virtual
so EF can override them.
When you call integration.LogEntries.Add
, the getter of your LogEntries
property is being called which triggers lazy loading operation.
You can temporarily disable lazy loading using the following code:
context.Configuration.LazyLoadingEnabled = false;
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
add a comment |
up vote
0
down vote
In order to enable lazy loading, EF creates proxy-classes derived from your model. In these classes it overrides the navigation properties to implement lazy loading. This is why your navigation properties should be defined virtual
so EF can override them.
When you call integration.LogEntries.Add
, the getter of your LogEntries
property is being called which triggers lazy loading operation.
You can temporarily disable lazy loading using the following code:
context.Configuration.LazyLoadingEnabled = false;
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
In order to enable lazy loading, EF creates proxy-classes derived from your model. In these classes it overrides the navigation properties to implement lazy loading. This is why your navigation properties should be defined virtual
so EF can override them.
When you call integration.LogEntries.Add
, the getter of your LogEntries
property is being called which triggers lazy loading operation.
You can temporarily disable lazy loading using the following code:
context.Configuration.LazyLoadingEnabled = false;
In order to enable lazy loading, EF creates proxy-classes derived from your model. In these classes it overrides the navigation properties to implement lazy loading. This is why your navigation properties should be defined virtual
so EF can override them.
When you call integration.LogEntries.Add
, the getter of your LogEntries
property is being called which triggers lazy loading operation.
You can temporarily disable lazy loading using the following code:
context.Configuration.LazyLoadingEnabled = false;
edited 17 hours ago
answered 17 hours ago
Kamyar
15.2k877152
15.2k877152
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
add a comment |
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Humm, I do want lazy-loading. I just don't want it when I'm adding?
– Kieren Johnstone
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
Can't think of any workaround since it's all done under the hood without letting the programmer to change the behavior. Couldn't you turn lazy loading off just for this context's instance?
– Kamyar
17 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237107%2fef6-lazy-loading-loads-entire-collection-when-adding%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Lazy loading is triggered by a property getter. And
integration.LogEntries.Add(…)
includes property getterintegration.LogEntries
.– Ivan Stoev
15 hours ago
if you don't want lazy loading to happen, either disable it during the add procedure, do not track the item or attach the navigation property items to the sets and set their navigation property/FK to the correct values instead.
– DevilSuichiro
13 hours ago