Skip to content

Instantly share code, notes, and snippets.

@tekiegirl
Last active August 29, 2015 14:08
Show Gist options
  • Save tekiegirl/c19deb492ec3fff0b5a7 to your computer and use it in GitHub Desktop.
Save tekiegirl/c19deb492ec3fff0b5a7 to your computer and use it in GitHub Desktop.
// Seed code from Configuration.cs
protected override void Seed(DAL.Models.Context context)
{
// This method will be called after migrating to the latest version.
// Default Customers - create
var customers = new[]{
new Customer { Name = "cust_a" },
new Customer { Name = "cust_b" },
new Customer { Name = "cust_c" }
};
context.Customers.AddOrUpdate(r => r.Name, customers[0], customers[1], customers[2]);
context.SaveChanges();
// Default Licenses - create
var licenses = new[]{
new License { DateCreated = DateTime.Now.AddDays(-3), CustomerId = customers[0].CustomerId },
new License { DateCreated = DateTime.Now.AddDays(-2), CustomerId = customers[1].CustomerId },
new License { DateCreated = DateTime.Now.AddDays(-1), CustomerId = customers[2].CustomerId }
};
context.Licenses.AddOrUpdate(r => r.DateCreated, licenses[0], licenses[1], licenses[2]);
context.SaveChanges();
// Default Products - create
var products = new[]{
new Product { Name = "prod_a" },
new Product { Name = "prod_b" },
new Product { Name = "prod_c" }
};
context.Products.AddOrUpdate(r => r.Name, products[0], products[1], products[2]);
context.SaveChanges();
// Default ProductLicenses - create, and add default licenses
var productLicenses = new[]{
new ProductLicense { LicenceType = LicenseType.Annual, StartDate = DateTime.Now, LicenseId = licenses[0].LicenseId, ProductId = products[0].ProductId },
new ProductLicense { LicenceType = LicenseType.Monthly, StartDate = DateTime.Now, LicenseId = licenses[1].LicenseId, ProductId = products[1].ProductId },
new ProductLicense { LicenceType = LicenseType.PAYG, StartDate = DateTime.Now, LicenseId = licenses[2].LicenseId, ProductId = products[2].ProductId }
};
context.ProductLicenses.AddOrUpdate(r => r.LicenceType, productLicenses[0], productLicenses[1], productLicenses[2]);
context.SaveChanges();
// Default Users - create
var users = new[]{
new User { Username = "user@_a.com", Password = GenerateDefaultPasswordHash(), Salt = _defaultSalt, CustomerId = customers[0].CustomerId },
new User { Username = "user@_b.co.uk", Password = GenerateDefaultPasswordHash(), Salt = _defaultSalt, CustomerId = customers[1].CustomerId },
new User { Username = "user@_c.com", Password = GenerateDefaultPasswordHash(), Salt = _defaultSalt, CustomerId = customers[2].CustomerId }
};
context.Users.AddOrUpdate(r => r.Username, users[0], users[1], users[2]);
context.SaveChanges();
// Default Roles - create
var roles = new[]{
new Role { Name = "Super_a", RoleId = users[0].UserId },
new Role { Name = "User_b", RoleId = users[1].UserId },
new Role { Name = "User_c", RoleId = users[2].UserId }
};
context.Roles.AddOrUpdate(r => r.Name, roles[0], roles[1], roles[2]);
context.SaveChanges();
// Default Permissions - create, and add default product licenses and roles
var permissions = new[]{
new Permission { Name = "Super_a", ProductLicenseId = productLicenses[0].ProductLicenseId, RoleId = roles[0].RoleId },
new Permission { Name = "Access_b", ProductLicenseId = productLicenses[1].ProductLicenseId, RoleId = roles[1].RoleId },
new Permission { Name = "Access_c", ProductLicenseId = productLicenses[2].ProductLicenseId, RoleId = roles[2].RoleId }
};
context.Permissions.AddOrUpdate(r => r.Name, permissions[0], permissions[1], permissions[2]);
context.SaveChanges();
}
// All the Models
public class Customer : Entity
{
[Key]
public int CustomerId { get; set;}
[Required(ErrorMessage = "Username is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(500)]
public string Name { get; set; }
// Relationships
public virtual ICollection<User> Users { get; set; } // one Customer has many users
//[ForeignKey("License")]
public int LicenseId { get; set; }
[ForeignKey("LicenseId")]
public virtual License License { get; set; } // one Customer has one License
}
public class License : Entity
{
[Key, ForeignKey("Customer")]
public int LicenseId { get; set; }
[Required(ErrorMessage = "Date Created name is required")]
public DateTime DateCreated { get; set; }
//Relationships
public virtual ICollection<ProductLicense> ProductLicenses { get; set; } // one License has many ProductLicenses
//[ForeignKey("Customer")]
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; } // one Customer has one License
}
public class ProductLicense : Entity
{
[Key, ForeignKey("Permission")]
public int ProductLicenseId { get; set; }
[Required(ErrorMessage = "Licence Type is required")]
public LicenseType LicenceType { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
// Relationships
//[ForeignKey("Permission")]
public int PermissionId { get; set; }
[ForeignKey("PermissionId")]
public virtual Permission Permission { get; set; } // one Permission has one ProductLicense
//[ForeignKey("Product")]
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; } // one ProductLicense has one Product
//[ForeignKey("License")]
public int LicenseId { get; set; }
[ForeignKey("LicenseId")]
public virtual License License { get; set; } // one License has many ProductLicenses
}
public class User : Entity
{
[Key]
public int UserId { get; set; }
[Required(ErrorMessage="Username is required")]
[Column(TypeName="nvarchar")]
[MaxLength(256)]
[Index("IX_Username", IsUnique = true)]
public string Username { get; set; }
[Required(ErrorMessage = "Password is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(2000)]
public string Password { get; set; }
[Required(ErrorMessage = "Salt is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(1000)]
public string Salt { get; set; }
// Relationships
//[ForeignKey("Customer")]
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; } // one Customer has many users
public virtual ICollection<Role> Roles { get; set; } // one User has many roles
}
public class Role : Entity
{
[Key]
public int RoleId { get; set; }
[Required(ErrorMessage = "Role name is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(500)]
public string Name { get; set; }
// Relationships
//[ForeignKey("User")]
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; } // one User has many roles
public virtual ICollection<Permission> Permissions { get; set; } // one Role has many Permissions
}
public class Permission : Entity
{
[Key]
public int PermissionId { get; set; }
[Required(ErrorMessage = "Permission name is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(500)]
public string Name { get; set; }
// Relationships
//[ForeignKey("Role")]
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public virtual Role Role { get; set; } // one Role has many Permissions
//[ForeignKey("ProductLicense")]
public int ProductLicenseId { get; set; }
[ForeignKey("ProductLicenseId")]
public virtual ProductLicense ProductLicense { get; set; } // one Permission has one ProductLicense
}
public class Product : Entity
{
[Key, ForeignKey("ProductLicense")]
public int ProductId { get; set; }
[Required(ErrorMessage = "Product name is required")]
[Column(TypeName = "nvarchar")]
[MaxLength(250)]
public string Name { get; set; }
//[ForeignKey("ProductLicense")]
public int ProductLicenseId { get; set; }
[ForeignKey("ProductLicenseId")]
public virtual ProductLicense ProductLicense { get; set; } // one ProductLicense has one Product
}
public enum LicenseType
{
Annual,
Monthly,
PAYG
}
public class Entity
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment