Skip to content

Instantly share code, notes, and snippets.

@Ziied
Created December 20, 2013 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ziied/8062106 to your computer and use it in GitHub Desktop.
Save Ziied/8062106 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FunWithBreezeJs.Data
{
public class CoolConfContext : DbContext
{
public DbSet<Presentation> Presentations { get; set; }
public DbSet<Speaker> Conferenciers { get; set; }
public DbSet<Attendee> Participants { get; set; }
public DbSet<PresentationAttendee> Participations { get; set; }
static CoolConfContext()
{
Database.SetInitializer<CoolConfContext>(new CoolConfDatabaseInitializer());
}
public CoolConfContext()
: base("CoolConfContext")
{
}
}
public class Presentation
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public string Theme { get; set; }
public DateTime Date { get; set; }
public int SpeakerId { get; set; }
[ForeignKey("SpeakerId")]
public virtual Speaker Speaker { get; set; }
}
public class Speaker
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public string Image { get; set; }
public virtual ICollection<Presentation> Presentations { get; set; }
}
public class Attendee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<PresentationAttendee> Participations { get; set; }
}
public class PresentationAttendee
{
[Key, Column(Order = 0)]
public int AttendeeId { get; set; }
[Key, Column(Order = 1)]
public int PresentationId { get; set; }
public DateTime DateInscription { get; set; }
[ForeignKey("AttendeeId")]
public virtual Attendee Attendee { get; set; }
[ForeignKey("PresentationId")]
public virtual Presentation Presentation { get; set; }
}
public class CoolConfDatabaseInitializer : DropCreateDatabaseAlways<CoolConfContext>
{
protected override void Seed(CoolConfContext context)
{
var speaker = new Speaker
{
Name = "Harpo Marx",
Bio = "Hoc inmaturo interitu ipse quoque sui pertaesus excessit e vita aetatis nono anno atque vicensimo cum quadriennio imperasset. natus apud Tuscos in Massa Veternensi, patre Constantio Constantini fratre imperatoris, matreque Galla sorore Rufini et Cerealis, quos trabeae consulares nobilitarunt et praefecturae.",
Presentations = new List<Presentation>()
};
var presentation1 = new Presentation
{
Theme = "SPA",
Title = "Fun With BreezeJS",
Speaker = speaker,
Date = DateTime.UtcNow.AddRandomDays(7)
};
var presentation2 = new Presentation
{
Theme = "SPA",
Title = "Intro to Durandal",
Speaker = speaker,
Date = DateTime.UtcNow.AddRandomDays(7)
};
speaker.Presentations.Add(presentation1); speaker.Presentations.Add(presentation2);
var speaker2 = new Speaker
{
Name = "Groucho Marx",
Bio = "Hoc inmaturo interitu ipse quoque sui pertaesus excessit e vita aetatis nono anno atque vicensimo cum quadriennio imperasset. natus apud Tuscos in Massa Veternensi, patre Constantio Constantini fratre imperatoris, matreque Galla sorore Rufini et Cerealis, quos trabeae consulares nobilitarunt et praefecturae.",
Presentations = new List<Presentation>(),
};
var presentation3 = new Presentation
{
Theme = "erlang",
Title = "Intro to erlang",
Speaker = speaker2,
Date = DateTime.UtcNow.AddRandomDays(7)
};
var presentation4 = new Presentation
{
Theme = "erlang",
Title = "concurrency in erlang",
Speaker = speaker2,
Date = DateTime.UtcNow.AddRandomDays(7)
};
var speaker3 = new Speaker
{
Name = "Chico Marx,",
Bio = "Hoc inmaturo interitu ipse quoque sui pertaesus excessit e vita aetatis nono anno atque vicensimo cum quadriennio imperasset. natus apud Tuscos in Massa Veternensi, patre Constantio Constantini fratre imperatoris, matreque Galla sorore Rufini et Cerealis, quos trabeae consulares nobilitarunt et praefecturae.",
Presentations = new List<Presentation>()
};
var presentation5 = new Presentation
{
Theme = "rabbitmq",
Title = "Intro to rabbitmq",
Speaker = speaker3,
Date = DateTime.UtcNow.AddRandomDays(7)
};
var presentation6 = new Presentation
{
Theme = "Entity framework",
Title = "New stuff on Entity framework 6",
Speaker = speaker3,
Date = DateTime.UtcNow.AddRandomDays(7)
};
speaker3.Presentations.Add(presentation5); speaker3.Presentations.Add(presentation6);
context.Conferenciers.AddRange(new[] { speaker, speaker2, speaker3 });
context.Presentations.AddRange(new[] { presentation1, presentation2, presentation3, presentation4, presentation5, presentation6 });
context.SaveChanges();
}
}
public static class DateTimeExtensions
{
static Random _random = new Random((int)DateTime.UtcNow.Ticks & 0x0000FFFF);
public static DateTime AddRandomDays(this DateTime datetime, int count)
{
var val = _random.Next(datetime.Day, datetime.Day + count + 1);
return datetime.AddDays(val);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment