Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Entity has two properties which both reference the same entity type in one-to-many relationship
  2. public class Recording
  3. {
  4.     [Key]
  5.     public virtual int Id { get; set; }
  6.  
  7.     //... other properties not shown here
  8.  
  9.     public virtual int RecordingLocationId { get; set; }
  10.     public virtual WorkLocation RecordingLocation { get; set; }
  11.  
  12.     public virtual int EditingLocationId { get; set; }
  13.     public virtual WorkLocation EditingLocation { get; set; }
  14. {
  15.  
  16.  
  17. public class WorkLocation
  18. {
  19.     [Key]
  20.     public virtual int Id { get; set; }
  21.     public virtual WorkLocationType Type { get; set; }
  22.     public virtual string Description { get; set; }
  23.     public virtual LogicalStatus Status { get; set; }
  24. }
  25.  
  26. // I'll use this on the front-end to filter a selection list
  27. // but don't necessarily assume a Work Location is bound to only items of this type
  28. public enum WorkLocationType
  29. {
  30.     RecordingLocation,
  31.     EditingLocation,
  32.     MasteringLocation
  33. }
  34.        
  35. public class MyContext : DbContext
  36. {
  37.     protected override void OnModelCreating(DbModelBuilder modelBuilder)
  38.     {
  39.         modelBuilder.Entity<Recording>()
  40.             .HasRequired(r => r.RecordingLocation)
  41.             .WithMany()
  42.             .HasForeignKey(f => f.RecordingLocationId)
  43.             .WillCascadeOnDelete(false);
  44.  
  45.         modelBuilder.Entity<Recording>()
  46.             .HasRequired(r => r.EditingLocation)
  47.             .WithMany()
  48.             .HasForeignKey(f => f.EditingLocationId)
  49.             .WillCascadeOnDelete(false);
  50.     }
  51. }