Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Entity Framework save related classes automatically?

Let's assume that we have such classes

public class A{
    string someField { get; set; }
    public virtual B B {get; set; }
}

public class B {
   int someIntField {get; set; }

   [ForeignKey("Id")]
   [Required]
   public virtual A A { get; set; } 
}

In code I create new instances for both of them and making relation like:

A a = new A () { someField = "abcd"};
B b = new B () { someIntField = 42 };

A.B = b;
B.A = a;

Should I using DBContext to save both classes like that:

using (var db = new myDbContext()) {
    myDbContext.As.Add(A);
    myDbContext.Bs.Add(B);
    myDBContext.SaveChanges();
}

Or saving it like that:

using (var db = new myDbContext()) {
    myDbContext.As.Add(A);
    myDbContext.SaveChanges();
}

is enough to store related objects into database?

like image 920
h__ Avatar asked Oct 23 '13 15:10

h__


People also ask

How does Entity Framework save data?

Insert Data Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context. Students. Add(std) adds a newly created instance of the Student entity to a context with Added EntityState.

How do I save changes in Entity Framework?

Entity Framework Core Save Changes to the database using the SaveChanges method of DbContext. When we use the SaveChanges it prepares the corresponding insert , update , delete queries. It then wraps them in a Transaction and sends them to the database. If any of the queries fails all the statements are rolled back.

When would you use EF6 vs EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

What does AsNoTracking do in Entity Framework?

The AsNoTracking() extension method returns a new query and the returned entities will not be cached by the context (DbContext or Object Context). This means that the Entity Framework does not perform any additional processing or storage of the entities that are returned by the query.


2 Answers

From your example, if you instantiate new entity objects and then assign the child to the parent object's property, then EF will actually create a new child instance (record) and map it to the parent as a reference.

Your code snippet is a bit confusing because you've got reference from A to B and B to A but consider the following:

if you had 2 entities:

public class A
{
    public int Id;
}

public class B
{
    public int Id;
    public A A;
}

and when saving you did something like this:

A a = new A();
B  b = new B();
b.A = a;
dbcontext.SaveChanges();

then EF will create a new record for A, a new record for B and add the reference of newly created A record to B.

See this post for further details

like image 131
amythn04 Avatar answered Oct 12 '22 16:10

amythn04


Saving the object that contains the other object is enough. It will automatically persist the changes to contained objects as well.

You could just create a small example and see for yourself what works.

like image 26
Jeroen Vannevel Avatar answered Oct 12 '22 16:10

Jeroen Vannevel