using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
public class BloggingContext : DbContext
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Author> Authors { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
public int AuthorId {get; set;}
public string Name {get; set;}
public int AuthorId {get; set;}
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; } = new List<Post>();
public Author Author {get; set;}
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; }
public static void Main()
using var context = new BloggingContext();
var blog = new Blog { BlogId = 1, Url = "aaa" };
context.Blogs.Attach(blog);
blog.Author = new Author { Name = "Daniel Turan" };
var changedEntries = context.ChangeTracker
.Where(e => e.State != EntityState.Unchanged)
foreach (var entry in changedEntries.Where(e => e.State == EntityState.Modified))
foreach(var p in entry.Properties.Where(p => p.IsModified))
Console.WriteLine($"{p.Metadata.Name}: {p.CurrentValue}");