Friday, 14 February 2020

EF core basics


EF Core some common useful operations

Making Relations between entities:

Types of Entities in EF Core:
  • Dependent entity: This is the entity that contains the foreign key properties. Sometimes referred to as the 'child' of the relationship.
  • Principal entity: This is the entity that contains the primary/alternate key properties. Sometimes referred to as the 'parent' of the relationship.
  • Principal key: The properties that uniquely identify the principal entity. This may be the primary key or an alternate key.
  • Foreign key: The properties in the dependent entity that are used to store the principal key values for the related entity.
  • Navigation property: A property defined on the principal and/or dependent entity that references the related entity.
    • Collection navigation property: A navigation property that contains references to many related entities.
    • Reference navigation property: A navigation property that holds a reference to a single related entity.
    • Inverse navigation property: When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.
public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
  • Post is the dependent entity
  • Blog is the principal entity
  • Blog.BlogId is the principal key (in this case it is a primary key rather than an alternate key)
  • Post.BlogId is the foreign key
  • Post.Blog is a reference navigation property
  • Blog.Posts is a collection navigation property
  • Post.Blog is the inverse navigation property of Blog.Posts (and vice versa)
Relationship between entities

One-to-Many

public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }


One-to-one

public class Blog { public int BlogId { get; set; } public string Url { get; set; } public BlogImage BlogImage { get; set; } } public class BlogImage { public int BlogImageId { get; set; } public byte[] Image { get; set; } public string Caption { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }

Many to Many:

public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public List<PostTag> PostTags { get; set; } } public class Tag { public string TagId { get; set; } public List<PostTag> PostTags { get; set; } } public class PostTag { public int PostId { get; set; } public Post Post { get; set; } public string TagId { get; set; } public Tag Tag { get; set; } }

Operations on Entities:

Saving Data


Each context instance has a ChangeTracker that is responsible for keeping track of changes that need to be written to the database. As you make changes to instances of your entity classes, these changes are recorded in the ChangeTracker and then written to the database when you call SaveChanges. The database provider is responsible for translating the changes into database-specific operation.

Adding Data

DBSet.Add method is used to add data, after adding data we need to call SaveChanges() method.

for example, we have a entity called Employee, to add employee object to database we need to write following code:

context.Employees.Add(objEmployee);
context.SaveChanges();

Updating Data

To Update data, we firstly need to search required entity which we want to update and then need to change it's properties and then need to save it.

var emp = context.Employees.First(); emp.FirstName= "New Name"; context.SaveChanges();

Deleting Data

var emp = context.Employees.First();
context.Employees.Remove(emp); context.SaveChanges();

Adding a new entities hierarchically


using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://blogs.msdn.com/dotnet", Posts = new List<Post> { new Post { Title = "Intro to C#" }, new Post { Title = "Intro to VB.NET" }, new Post { Title = "Intro to F#" } } }; context.Blogs.Add(blog); context.SaveChanges(); }

Use the EntityEntry.State property to set the state of just a single entity. For example, context.Entry(blog).State = EntityState.Modified.

Adding a related entity

adding a new post to the post collection of a blog

using (var context = new BloggingContext()) { var blog = context.Blogs.Include(b => b.Posts).First(); var post = new Post { Title = "Intro to EF Core" }; blog.Posts.Add(post); context.SaveChanges(); }

Changing relationships

In the following example, the post entity is updated to belong to the new blog entity because its Blog navigation property is set to point to blog. Note that blog will also be inserted into the database because it is a new entity that is referenced by the navigation property of an entity that is already tracked by the context (post).

using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://blogs.msdn.com/visualstudio" }; var post = context.Posts.First(); post.Blog = blog; context.SaveChanges(); }

Removing relationships

In the following example, a cascade delete is configured on the relationship between Blog and Post, so the post entity is deleted from the database.

using (var context = new BloggingContext()) { var blog = context.Blogs.Include(b => b.Posts).First(); var post = blog.Posts.First(); blog.Posts.Remove(post); context.SaveChanges(); }


No comments:

Post a Comment

How to create and use middleware in asp.net core

Middleware is piece of code that's assembled into an app pipeline to handle requests and responses.  Each middleware component in the re...