8.5. CRUD Operations in EF

When you utilize EF, a connection to a database called an object context is created. When new entities are created or properties change, they are not written back to the database immediately; rather, a flag is set on the entity to indicate that it has changed, but not saved to the database (a dirty state). Changes are then saved back to the database when the SaveChanges() method is called on the object context.

8.5.1. Creating

The following example shows how to create a new Film entity with a related FilmShowing entity and save the new items back to the database:

BookEntities ctx = new BookEntities();

Film NewFilm = new Film();
NewFilm.Title = "New film";
NewFilm.Description = "New film";
NewFilm.Length = 300;

FilmShowing NewFilmShowing = new FilmShowing();
NewFilmShowing.Screen = 5;
NewFilmShowing.ShowingDate = System.DateTime.Now;
NewFilm.FilmShowings.Add(NewFilmShowing);

ctx.AddObject("Films", NewFilm);
ctx.SaveChanges();

You can also use the CreateFilm() method on the Film class itself to perform the same action:

BookEntities ctx = new BookEntities();
Film NewFilm = Film.CreateFilm(0);
NewFilm.Title = "New film2";
NewFilm.Description = "New film";
NewFilm.Length = 300;

ctx.AddToFilms(NewFilm);
ctx.SaveChanges();

Notice how in the previous example we created a film with ID 0 that EF automatically incremented when we saved it back to the database.

Entities can be customized by using partial classes. So, in the previous example, you might want to add some new methods to your Film entity. This could be achieved as follows:

public partial class Film : EntityObject
{
    public void CheckAvailability()
    { }

    public void PrintTicket()
    { }
}

8.5.2. Updating

To update an EF object, simply retrieve an entity, make your changes, and then call the SaveChanges() method on the DataContext:

BookEntities ctx = new BookEntities();

Film FilmToUpdate = ctx.Films.Where("it.FilmID = 3").First();
FilmToUpdate.Title = "New updated title";
ctx.SaveChanges();

CONCURRENCY

EF supports a number of different concurrency options. The default is optimistic concurrency (last write wins). Other advanced concurrency options enable EF to check if underlying data has changed before writing it back.


8.5.3. Deleting

Deleting entities is very easy. You must remember, however, to delete related child objects if referential integrity is enforced in your database (note that the example database doesn't enforce referential integrity, which makes it easier to play with).

BookEntities ctx = new BookEntities();

ctx.Films.DeleteObject(ctx.Films.Where("it.FilmID = 5").First());

foreach (FilmShowing fs in ctx.FilmShowings.Where("it.FilmID = 5"))
{
    ctx.FilmShowings.DeleteObject(fs);
}

I hope you now have a basic idea of what EF can provide for you:

ctx.SaveChanges();

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.95