Skip to content

Instantly share code, notes, and snippets.

@happygrizzly
Last active August 29, 2015 14:13
Show Gist options
  • Save happygrizzly/ffe04d584aca4a037fa4 to your computer and use it in GitHub Desktop.
Save happygrizzly/ffe04d584aca4a037fa4 to your computer and use it in GitHub Desktop.
public class RoleResourceActivity {
public Int32 ResourceId { get; set; }
public virtual Resource Resource { get; set; }
public Int32 ActivityId { get; set; }
public virtual Activity Activity { get; set; }
public Int32 RoleId { get; set; }
public Role Role { get; set; }
}
// User-Role relationship cardinality is many to many:
public class User {
public Int32 Id { get; set; }
public virtual ISet<Role> Roles { get; set; }
// ctors are omitted
}
public class Role {
public Int32 Id { get; set; }
public String Name { get; set; }
public virtual ISet<User> Users { get; set; }
public virtual ISet<RoleResourceActivity> RoleResourceActivities { get; set; }
// ctors are omitted
}
public class Activity {
public Int32 Id { get; set; }
public String Name { get; set; }
public virtual ISet<RoleResourceActivity> RoleResourceActivities { get; set; }
public Activity(String name) : this() {
this.Name = name;
}
protected Activity() {
this.RoleResourceActivities = new HashSet<RoleResourceActivity>();
}
}
public class Resource {
public Int32 Id { get; set; }
public String Name { get; set; }
public virtual ISet<RoleResourceActivity> RoleResourceActivities { get; set; }
public Resource(String name) : this() {
this.Name = name;
}
protected Resource() {
this.RoleResourceActivities = new HashSet<RoleResourceActivity>();
}
}
public class AppDbContext : DbContext {
public DbSet<User> Users { get; set; }
public DbSet<RoleResourceActivity> RoleResourceActivities { get; set; }
public DbSet<Activity> Activities { get; set; }
public DbSet<Resource> Resources { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Activity>().HasKey(i => i.Id);
modelBuilder.Entity<Resource>().HasKey(i => i.Id);
modelBuilder.Entity<User>().HasKey(i => i.Id);
modelBuilder.Entity<RoleResourceActivity>().HasKey(i => new {i.ResourceId, i.ActivityId, i.RoleId});
base.OnModelCreating(modelBuilder);
}
public AppDbContext() : base("AppDb") {
Database.SetInitializer<AppDbContext>(new AppDbContextInitializer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment