locked
Entity splitting into two tables RRS feed

  • Question

  • I have a Users table and a UserProfiles table. A user has either zero or only one User profile. (i.e one to one relationship) can someone help me use EF4.1 fluent API to Map the Users Entity to the both Users and UserProfiles tables. Below is the table details. Also Users and AuthProvider have a one to Many relationship.

     

    Users Table has the columns(UserId(PK), UserName, Email, AuthProviderId(FK))

    UserProfiles Table has columns(UserId(PK/FK), FirstName, MiddleName,LastName)

    AuthProvider Table has columns (AuthProviderId(PK), AuthName)

     

    After doing all this am getting this error :

    System.InvalidOperationException : The property 'AuthProvider' on type 'User' cannot be mapped because it has been explicitly excluded from the model.

     

    Can some one please help.

     

      public class User {
        public User() {
          Id = Guid.NewGuid();
        }
        public virtual Guid Id { get; private set; }
    
        public virtual string Username { get; set; }
        public virtual string Email { get; set; }
        public virtual AuthProvider AuthProvider { get; set; }
    
        public virtual string FirstName { get; set; }
        public virtual string MiddleName { get; set; }
        public virtual string LastName { get; set; }
      }
    
    public class AuthProvider {
        public AuthProvider() {
          Id = Guid.NewGuid();
        }
        public virtual Guid Id { get; private set; }
        public virtual string Name { get; set; }   
        public virtual ICollection<User> Users { get; set; }
      }
    
    // This is the Mappings
     public class UserConfiguration : EntityTypeConfiguration<User> {
        public UserConfiguration() {
          HasKey(x => x.Id).Property(x => x.Id).HasColumnName("UserId").IsRequired();
          Property(x => x.Username).HasColumnName("Username").IsRequired();
          Property(x => x.Email).HasColumnName("Email").IsRequired();
    
          Property(x => x.FirstName).HasColumnName("FirstName").IsOptional();
          Property(x => x.MiddleName).HasColumnName("MiddleName").IsOptional();
          Property(x => x.LastName).HasColumnName("LastName").IsOptional();
    
          HasRequired(x => x.AuthProvider).WithMany(x => x.Users).Map(x => x.MapKey("AuthProviderId"));
    
          Map(mc => {
            mc.Properties(x => new {
              x.Id,
              x.Username,
              x.Email,
              x.AuthProvider
            });
            mc.ToTable("Users");
          });
    
          Map(mc => {
            mc.Properties(x => new { x.Id, x.FirstName, x.MiddleName, x.LastName });
            mc.ToTable("UserProfiles");
          });
        }
      }
    
    
     public class AuthProviderConfiuration : EntityTypeConfiguration<AuthProvider> {
        public AuthProviderConfiuration() {
          ToTable("AuthProviders");
          HasKey(x => x.Id).Property(x => x.Id).HasColumnName("AuthProviderId").IsRequired();
          Property(x => x.Name).HasColumnName("ProviderName").IsRequired();
    
        }
      }
    
    
    



    Thanks
    Sunday, June 26, 2011 5:23 PM

All replies

  • Hello,

    remove x.AuthProvider from mapping to Users table. You cannot map navigation property.

       Map(mc => {
        mc.Properties(x => new {
         x.Id,
         x.Username,
         x.Email
        });
        mc.ToTable("Users");
       });

    Best regards,
    Ladislav

    Monday, June 27, 2011 7:35 AM