locked
Entity Framework Mapping config on OnModelCreating RRS feed

  • Question

  • User-2071863390 posted

    Hi, I have two tables in my database:

    SiteUsers & SiteUsersLogins

    I have the following code that maps my "Users" class to the SiteUsers table. A new row is entered into the SiteUsersLogins table on each login to the site. I would like to map the LastLogin property on the Users calls to the last login for that users.

    I'm not sure how to do the mapping of the LastLogin to the User within my UserMap or is it done within the OnModelCreating? Any pointers would be great?

    Thanks

    public class User
        {
            public int ID { get; set; }
            public string UserName { get; set; }
            public virtual DateTime LastLogin { get; set; }
        }
        public class Login
        { 
            public int ID { get; set; }
            public int UserID { get; set; }
            public DateTime DateAdded { get; set; }
        }
        public class UserMap : EntityTypeConfiguration<User>
        {
            public UserMap()
            {
                this.HasKey(t => t.ID);
                this.ToTable("SiteUsers");
                this.Property(t => t.UserName).HasColumnName("UserName");
            }
        }
        public class UserContext : DbContext
        {
            public UserContext()
                : base("MyConString")
            {
                Database.SetInitializer<UserContext>(null);
            }
    
            public virtual DbSet<User> Users { get; set; } 
            public virtual DbSet<Login> Logins { get; set; } 
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new UserMap());
                base.OnModelCreating(modelBuilder);
            }
        }

    Sunday, April 5, 2015 1:43 PM

All replies

  • User-821857111 posted

    So you want to map your User entity to two different tables? You can do this using either in the OnModelCreating or in your Mapping class. It makes no practical difference. But since you have already decided to separate your mapping out to a class, you may as well stick with that approach for consistency:

    public class UserMap : EntityTypeConfiguration<User>
    {
        public UserMap()
        {
            this.HasKey(t => t.ID); 
            Map(m => {
                m.Properties(p => new {
                    p.Username
                });
                m.ToTable("Users");
            });
            Map(m => {
                m.Properties(p => new {
                     p.LastLogin
                });
                m.ToTable("SiteUsersLogins");
             });
        }
    }



    Monday, April 6, 2015 4:59 AM
  • User-2071863390 posted

    Thanks for the reply, so where would I select the last login date to assign to LastLogin when i do the below?

    public static User Get(int userID)
            { 
                User user = null;
                using(var db = new UserContext())
                {
                    user = db.Users
                        .Where(u => u.ID ==userID)
                        .SingleOrDefault();              
                }
                return user;
            }

    Thanks again

    Monday, April 6, 2015 5:15 AM
  • User-821857111 posted

    You can do this:

    user.LastLoginDate = DateTime.Now;
    db.SaveChanges();

    Monday, April 6, 2015 1:39 PM
  • User-2071863390 posted

    My SiteUserLogin table contains many rows for each user (everytime a user logs in a new row is created), where do I select the last login date for the user in the user mapping?

    thanks

    Monday, April 6, 2015 2:06 PM
  • User-821857111 posted

    You'll need to change the data type to a collection of DateTime object if there are many LastLogin values per user. As it currently stands, there can only be one value for that property and one row per user.

    Monday, April 6, 2015 2:35 PM
  • User-2071863390 posted

    Ideally I'd like to keep it as DateTime object as I only want to return/get the last login date. Is this possible or do I have to change to collections an then get the last one? If I was doing this via SQL I would do in line select top 1 DateAdded order by descending etc... as LastLoginDate etc..

    Monday, April 6, 2015 2:42 PM
  • User-821857111 posted

    EF will expect one row that needs updating if you have 1 DateTime property. It will only see an insert if you have a collection. Logically, you are storing a collection of LastLoginDates for each user so to my mind, a collection makes more sense. You can use LINQ to get the most recent one in the same way as you do with SQL:

    var lastLogin = user.LastLoginDate.OrderByDescending(l => l).First();

    Monday, April 6, 2015 11:26 PM