Giter Club home page Giter Club logo

entityframework.triggers's Introduction

EntityFramework.Triggers

Add triggers to your entities with insert, update, and delete events. There are three events for each: before, after, and upon failure.

NuGet package listed on nuget.org at https://www.nuget.org/packages/EntityFramework.Triggers/

async/await supported

Usage

To use triggers on your entities, you simply need to have your entities inherit from ITriggerable, and override SaveChanges() in your DbContext class to call the SaveChangesWithTriggers() extension method. For async/await functionality, override and call the ...Async methods respectively.

using System;
using System.Data.Entity;
using System.Threading;
using System.Threading.Tasks;
using EntityFramework.Triggers;

namespace Example {
	class Program {
		private class Person : ITriggerable {
			public Int64 Id { get; protected set; }
			public DateTime InsertDateTime { get; protected set; }
			public DateTime UpdateDateTime { get; protected set; }
			public String FirstName { get; set; }
			public String LastName { get; set; }
			public Boolean IsDeleted { get; set; }
			public Person() {
				this.Triggers().Inserting += entry => { entry.Entity.InsertDateTime = entry.Entity.UpdateDateTime = DateTime.Now; };
				this.Triggers().Updating += entry => { entry.Entity.UpdateDateTime = DateTime.Now; };
				this.Triggers().Deleting += entry => {
					entry.Entity.IsDeleted = true;
					entry.Cancel(); // Cancels the deletion, but will persist changes with the same effects as EntityState.Modified
				};
			}
		}
		private class LogEntry {
			public Int64 Id { get; protected set; }
			public String Message { get; set; }
		}
		private class Context : DbContext {
			public DbSet<Person> People { get; set; }
			public DbSet<LogEntry> Log { get; set; }

			public override Int32 SaveChanges() {
				return this.SaveChangesWithTriggers(base.SaveChanges);
			}
			public override Task<Int32> SaveChangesAsync(CancellationToken cancellationToken) {
				return this.SaveChangesWithTriggersAsync(base.SaveChangesAsync, cancellationToken);
			}
		}
		private static void Main(string[] args) {
			var task = MainAsync(args);
			Task.WaitAll(task);
		}
		private static async Task MainAsync(string[] args) {
			using (var context = new Context()) {
				var nickStrupat = new Person {
					FirstName = "Nick",
					LastName = "Strupat"
				};
				nickStrupat.Triggers().Inserting += e => {
					((Context)e.Context).Log.Add(new LogEntry { Message = "Insert trigger fired for " + e.Entity.FirstName });
					Console.WriteLine("Inserting " + e.Entity.FirstName);
				};
				nickStrupat.Triggers().Updating += e => Console.WriteLine("Updating " + e.Entity.FirstName);
				nickStrupat.Triggers().Deleting += e => Console.WriteLine("Deleting " + e.Entity.FirstName);
				nickStrupat.Triggers().Inserted += e => Console.WriteLine("Inserted " + e.Entity.FirstName);
				nickStrupat.Triggers().Updated += e => Console.WriteLine("Updated " + e.Entity.FirstName);
				nickStrupat.Triggers().Deleted += e => Console.WriteLine("Deleted " + e.Entity.FirstName);

				context.People.Add(nickStrupat);
				context.SaveChanges();

				nickStrupat.FirstName = "Nicholas";
				context.SaveChanges();

				context.People.Remove(nickStrupat);
				await context.SaveChangesAsync();

				context.Database.Delete();
			}
		}
	}
}

entityframework.triggers's People

Contributors

nickstrupat avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.