What is Difference between Insert Data in Disconnected Scenario vs Insert Data in Connected Scenario, in EF...












0















What is difference between Insert Data in Disconnected Scenario vs Insert Data in Connected Scenario , in EF Core?



Sample of Insert Data in Disconnected:



     //Disconnected entity
var std = new Student(){ Name = "Bill" };

using (var context = new SchoolContext())
{
//1. Attach an entity to context with Added EntityState
context.Add<Student>(std);

//or the followings are also valid
// context.Students.Add(std);
// context.Entry<Student>(std).State = EntityState.Added;
// context.Attach<Student>(std);

//2. Calling SaveChanges to insert a new record into Students table
context.SaveChanges();
}


And sample Insert Data in Connected:



using (var context = new SchoolContext())
{
var std = new Student()
{
FirstName = "Bill",
LastName = "Gates"
};
context.Students.Add(std);

// or
// context.Add<Student>(std);

context.SaveChanges();
}


This question originated from this site for me => click here










share|improve this question

























  • You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:18













  • @PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

    – Ivan Stoev
    Nov 20 '18 at 12:21













  • @IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:25













  • @PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

    – Ivan Stoev
    Nov 20 '18 at 12:29











  • @IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

    – Panagiotis Kanavos
    Nov 20 '18 at 12:30
















0















What is difference between Insert Data in Disconnected Scenario vs Insert Data in Connected Scenario , in EF Core?



Sample of Insert Data in Disconnected:



     //Disconnected entity
var std = new Student(){ Name = "Bill" };

using (var context = new SchoolContext())
{
//1. Attach an entity to context with Added EntityState
context.Add<Student>(std);

//or the followings are also valid
// context.Students.Add(std);
// context.Entry<Student>(std).State = EntityState.Added;
// context.Attach<Student>(std);

//2. Calling SaveChanges to insert a new record into Students table
context.SaveChanges();
}


And sample Insert Data in Connected:



using (var context = new SchoolContext())
{
var std = new Student()
{
FirstName = "Bill",
LastName = "Gates"
};
context.Students.Add(std);

// or
// context.Add<Student>(std);

context.SaveChanges();
}


This question originated from this site for me => click here










share|improve this question

























  • You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:18













  • @PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

    – Ivan Stoev
    Nov 20 '18 at 12:21













  • @IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:25













  • @PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

    – Ivan Stoev
    Nov 20 '18 at 12:29











  • @IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

    – Panagiotis Kanavos
    Nov 20 '18 at 12:30














0












0








0


1






What is difference between Insert Data in Disconnected Scenario vs Insert Data in Connected Scenario , in EF Core?



Sample of Insert Data in Disconnected:



     //Disconnected entity
var std = new Student(){ Name = "Bill" };

using (var context = new SchoolContext())
{
//1. Attach an entity to context with Added EntityState
context.Add<Student>(std);

//or the followings are also valid
// context.Students.Add(std);
// context.Entry<Student>(std).State = EntityState.Added;
// context.Attach<Student>(std);

//2. Calling SaveChanges to insert a new record into Students table
context.SaveChanges();
}


And sample Insert Data in Connected:



using (var context = new SchoolContext())
{
var std = new Student()
{
FirstName = "Bill",
LastName = "Gates"
};
context.Students.Add(std);

// or
// context.Add<Student>(std);

context.SaveChanges();
}


This question originated from this site for me => click here










share|improve this question
















What is difference between Insert Data in Disconnected Scenario vs Insert Data in Connected Scenario , in EF Core?



Sample of Insert Data in Disconnected:



     //Disconnected entity
var std = new Student(){ Name = "Bill" };

using (var context = new SchoolContext())
{
//1. Attach an entity to context with Added EntityState
context.Add<Student>(std);

//or the followings are also valid
// context.Students.Add(std);
// context.Entry<Student>(std).State = EntityState.Added;
// context.Attach<Student>(std);

//2. Calling SaveChanges to insert a new record into Students table
context.SaveChanges();
}


And sample Insert Data in Connected:



using (var context = new SchoolContext())
{
var std = new Student()
{
FirstName = "Bill",
LastName = "Gates"
};
context.Students.Add(std);

// or
// context.Add<Student>(std);

context.SaveChanges();
}


This question originated from this site for me => click here







c# entity-framework entity-framework-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 12:28









Ivan Stoev

102k772125




102k772125










asked Nov 20 '18 at 12:06









AlirezaAviniAlirezaAvini

127




127













  • You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:18













  • @PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

    – Ivan Stoev
    Nov 20 '18 at 12:21













  • @IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:25













  • @PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

    – Ivan Stoev
    Nov 20 '18 at 12:29











  • @IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

    – Panagiotis Kanavos
    Nov 20 '18 at 12:30



















  • You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:18













  • @PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

    – Ivan Stoev
    Nov 20 '18 at 12:21













  • @IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

    – Panagiotis Kanavos
    Nov 20 '18 at 12:25













  • @PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

    – Ivan Stoev
    Nov 20 '18 at 12:29











  • @IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

    – Panagiotis Kanavos
    Nov 20 '18 at 12:30

















You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

– Panagiotis Kanavos
Nov 20 '18 at 12:18







You mean detached, not disconnected. In this case though there's no difference - new entities are detached by definition.

– Panagiotis Kanavos
Nov 20 '18 at 12:18















@PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

– Ivan Stoev
Nov 20 '18 at 12:21







@PanagiotisKanavos The term "disconnected" is used by site from the link. Also the EF Core documentation - Disconnected entities. Other than that, there is no difference for the entity being added. But for related entities through reference navigation properties there is a difference.

– Ivan Stoev
Nov 20 '18 at 12:21















@IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

– Panagiotis Kanavos
Nov 20 '18 at 12:25







@IvanStoev the very same thing was called Detached entities in the EF docs. Connected vs Disconnected referred to whether a cursor was kept open while working with data.

– Panagiotis Kanavos
Nov 20 '18 at 12:25















@PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

– Ivan Stoev
Nov 20 '18 at 12:29





@PanagiotisKanavos Yeah, but looks like the "official" term changed in EF Core. And since the question is about EF Core...

– Ivan Stoev
Nov 20 '18 at 12:29













@IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

– Panagiotis Kanavos
Nov 20 '18 at 12:30





@IvanStoev that doesn't mean the dictionary changed, what other ORMs mean when using those terms or what most people mean by disconnected/detached. Just google for EF detached entities. It does mean that a lot of people will get confused. The answer here is an example

– Panagiotis Kanavos
Nov 20 '18 at 12:30












1 Answer
1






active

oldest

votes


















0














In the connected scenario, the same instance of DbContext is used in retrieving and saving entities.



In the disconnected scenario, the DbContext is not aware of disconnected entities because entities were added or modified out of the scope of the current DbContext instance. So, you need to attach the disconnected entities to a context with appropriate EntityState in order to perform CUD (Create, Update, Delete) operations to the database.



I think connected scenario is best solution because we don't need to create new object of entity.






share|improve this answer
























  • The OP asks whether there's a difference when adding a new entity

    – Panagiotis Kanavos
    Nov 20 '18 at 12:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392675%2fwhat-is-difference-between-insert-data-in-disconnected-scenario-vs-insert-data-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In the connected scenario, the same instance of DbContext is used in retrieving and saving entities.



In the disconnected scenario, the DbContext is not aware of disconnected entities because entities were added or modified out of the scope of the current DbContext instance. So, you need to attach the disconnected entities to a context with appropriate EntityState in order to perform CUD (Create, Update, Delete) operations to the database.



I think connected scenario is best solution because we don't need to create new object of entity.






share|improve this answer
























  • The OP asks whether there's a difference when adding a new entity

    – Panagiotis Kanavos
    Nov 20 '18 at 12:29


















0














In the connected scenario, the same instance of DbContext is used in retrieving and saving entities.



In the disconnected scenario, the DbContext is not aware of disconnected entities because entities were added or modified out of the scope of the current DbContext instance. So, you need to attach the disconnected entities to a context with appropriate EntityState in order to perform CUD (Create, Update, Delete) operations to the database.



I think connected scenario is best solution because we don't need to create new object of entity.






share|improve this answer
























  • The OP asks whether there's a difference when adding a new entity

    – Panagiotis Kanavos
    Nov 20 '18 at 12:29
















0












0








0







In the connected scenario, the same instance of DbContext is used in retrieving and saving entities.



In the disconnected scenario, the DbContext is not aware of disconnected entities because entities were added or modified out of the scope of the current DbContext instance. So, you need to attach the disconnected entities to a context with appropriate EntityState in order to perform CUD (Create, Update, Delete) operations to the database.



I think connected scenario is best solution because we don't need to create new object of entity.






share|improve this answer













In the connected scenario, the same instance of DbContext is used in retrieving and saving entities.



In the disconnected scenario, the DbContext is not aware of disconnected entities because entities were added or modified out of the scope of the current DbContext instance. So, you need to attach the disconnected entities to a context with appropriate EntityState in order to perform CUD (Create, Update, Delete) operations to the database.



I think connected scenario is best solution because we don't need to create new object of entity.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 12:22









A.M. PatelA.M. Patel

629




629













  • The OP asks whether there's a difference when adding a new entity

    – Panagiotis Kanavos
    Nov 20 '18 at 12:29





















  • The OP asks whether there's a difference when adding a new entity

    – Panagiotis Kanavos
    Nov 20 '18 at 12:29



















The OP asks whether there's a difference when adding a new entity

– Panagiotis Kanavos
Nov 20 '18 at 12:29







The OP asks whether there's a difference when adding a new entity

– Panagiotis Kanavos
Nov 20 '18 at 12:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392675%2fwhat-is-difference-between-insert-data-in-disconnected-scenario-vs-insert-data-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith