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

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 1.69 KB  |  hits: 10  |  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. EF Upgrade from 4.0.0 to 4.3.1 causing strange errors
  2. public class ItemBase
  3. {
  4.     public DateTime Created { get; set; }
  5.     public int CreatedByUserID { get; set; }
  6.     public DateTime LastModified { get; set; }
  7.     public int LastModifiedByUserID { get; set; }
  8.  
  9.     public User CreatedBy { get; set; }
  10.     public User LastModifiedBy { get; set; }
  11.  
  12.     public ItemBase()
  13.     {
  14.         CreatedByUserID = -1;
  15.         LastModifiedByUserID = -1;
  16.         CreatedBy = null;
  17.         LastModifiedBy = null;
  18.     }
  19. }
  20.  
  21.  
  22. public class User : ItemBase
  23. {
  24.     public int UserID { get; set; }
  25.     public string LoginName { get; set; }
  26.     public string Password { get; set; }
  27.     public string EmailAddress { get; set; }
  28.     public string Firstname { get; set; }
  29.     public string Lastname { get; set; }
  30.     public string DisplayName { get; set; }
  31.  
  32.     public User() : base()
  33.     {
  34.         UserID = -1;
  35.         LoginName = String.Empty;
  36.         Password = String.Empty;
  37.         EmailAddress = String.Empty;
  38.         Firstname = String.Empty;
  39.         Lastname = String.Empty;
  40.         DisplayName = String.Empty;
  41.     }
  42. }
  43.  
  44.  
  45. protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
  46. {
  47.     base.OnModelCreating(modelBuilder);
  48.     modelBuilder.Entity<User>().Property(u => u.UserID).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);
  49. }
  50.        
  51. modelBuilder.Entity<User>().HasRequired(u => u.CreatedBy).WithMany().HasForeignKey(k => k.CreatedByUserID);
  52. modelBuilder.Entity<User>().HasRequired(u => u.LastModifiedBy).WithMany().HasForeignKey(k => k.LastModifiedByUserID);
  53.        
  54. public class MyContext : DbContext
  55. {
  56.     public MyContext()
  57.      : base("name=MyConnectionStringName")
  58.     {
  59.     }
  60. }