Created
May 13, 2013 22:41
-
-
Save pksorensen/5572128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace S_Innovations.Projects.EFModel.Migrations | |
{ | |
using System.Data.Entity.Migrations; | |
using System.Data.Entity.Migrations.Model; | |
using System.Data.Entity.Migrations.Sql; | |
public class AzureSqlGenerator : SqlServerMigrationSqlGenerator | |
{ | |
protected override void Generate(CreateTableOperation op) | |
{ | |
if ((op.PrimaryKey != null) && !op.PrimaryKey.IsClustered) | |
{ | |
op.PrimaryKey.IsClustered = true; | |
} | |
base.Generate(op); | |
} | |
} | |
internal sealed class Configuration : DbMigrationsConfiguration<S_Innovations.Projects.EFModel.IdentityContext> | |
{ | |
public Configuration() | |
{ | |
AutomaticMigrationsEnabled = false; | |
SetSqlGenerator("System.Data.SqlClient", new AzureSqlGenerator()); | |
} | |
protected override void Seed(S_Innovations.Projects.EFModel.IdentityContext context) | |
{ | |
// This method will be called after migrating to the latest version. | |
// You can use the DbSet<T>.AddOrUpdate() helper extension method | |
// to avoid creating duplicate seed data. E.g. | |
// | |
// context.People.AddOrUpdate( | |
// p => p.FullName, | |
// new Person { FullName = "Andrew Peters" }, | |
// new Person { FullName = "Brice Lambson" }, | |
// new Person { FullName = "Rowan Miller" } | |
// ); | |
// | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace S_Innovations.Projects.EFModel.Migrations | |
{ | |
using System; | |
using System.Data.Entity.Migrations; | |
public partial class IdentityContext : DbMigration | |
{ | |
public override void Up() | |
{ | |
CreateTable( | |
"dbo.Identities", | |
c => new | |
{ | |
Id = c.Int(nullable: false, identity: true), | |
IdentityProvider = c.String(), | |
NameIdentifier = c.String(), | |
DateAdded = c.DateTime(nullable: false, defaultValueSql: "GETDATE()"), | |
LastLoggedIn = c.DateTime(nullable: false, defaultValueSql: "GETDATE()"), | |
MemberId = c.Int(nullable: false), | |
}) | |
.PrimaryKey(t => t.Id) | |
.ForeignKey("dbo.Members", t => t.MemberId, cascadeDelete: true) | |
.Index(t => t.MemberId); | |
CreateTable( | |
"dbo.Members", | |
c => new | |
{ | |
Id = c.Int(nullable: false, identity: true), | |
FirstName = c.String(), | |
LastName = c.String(), | |
Email = c.String(), | |
}) | |
.PrimaryKey(t => t.Id); | |
CreateTable( | |
"dbo.Roles", | |
c => new | |
{ | |
Id = c.Int(nullable: false, identity: true), | |
RoleName = c.String(), | |
}) | |
.PrimaryKey(t => t.Id); | |
CreateTable( | |
"dbo.RoleMembers", | |
c => new | |
{ | |
Role_Id = c.Int(nullable: false), | |
Member_Id = c.Int(nullable: false), | |
}) | |
.PrimaryKey(t => new { t.Role_Id, t.Member_Id }) | |
.ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true) | |
.ForeignKey("dbo.Members", t => t.Member_Id, cascadeDelete: true) | |
.Index(t => t.Role_Id) | |
.Index(t => t.Member_Id); | |
} | |
public override void Down() | |
{ | |
DropForeignKey("dbo.Identities", "MemberId", "dbo.Members"); | |
DropForeignKey("dbo.RoleMembers", "Member_Id", "dbo.Members"); | |
DropForeignKey("dbo.RoleMembers", "Role_Id", "dbo.Roles"); | |
DropIndex("dbo.Identities", new[] { "MemberId" }); | |
DropIndex("dbo.RoleMembers", new[] { "Member_Id" }); | |
DropIndex("dbo.RoleMembers", new[] { "Role_Id" }); | |
DropTable("dbo.RoleMembers"); | |
DropTable("dbo.Roles"); | |
DropTable("dbo.Members"); | |
DropTable("dbo.Identities"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace S_Innovations.Projects.EFModel | |
{ | |
using System.ComponentModel.DataAnnotations; | |
using System.ComponentModel.DataAnnotations.Schema; | |
public class Identity | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public int Id { get; set; } | |
public string IdentityProvider { get; set; } | |
public string NameIdentifier { get; set; } | |
public Identity() | |
{ | |
} | |
[DatabaseGenerated(DatabaseGeneratedOption.Computed)] | |
public System.DateTime DateAdded { get; set; } | |
public System.DateTime LastLoggedIn { get; set; } | |
public int MemberId { get; set; } | |
[ForeignKey("MemberId")] | |
[Required] | |
public virtual Member Member { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace S_Innovations.Projects.EFModel | |
{ | |
public interface IMember | |
{ | |
int Id { get; set; } | |
Member Member { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace S_Innovations.Projects.EFModel | |
{ | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.ComponentModel.DataAnnotations.Schema; | |
public class Member | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public int Id { get; set; } | |
public Member() | |
{ | |
Identities = new HashSet<Identity>(); | |
Roles = new HashSet<Role>(); | |
} | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Email { get; set; } | |
public virtual ICollection<Identity> Identities { get; set; } | |
public virtual ICollection<Role> Roles { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment