Search code examples
asp.net-mvc-3entity-framework-4.1ef-code-firstfluent-interface

Mapping ICollection <string> using Fluent API


The FluentAPI can not create a relational model based on the code below:

public class Project
{
    public Guid ID { get; private set; }
    public string Name { get; set; }
    public virtual ICollection<string> Images { get; set; }
}

public class ProjectConfiguration : EntityTypeConfiguration<Project>
{
    public ProjectConfiguration()
    {
        HasKey(p => p.ID)
            .Property(p => p.ID)
                .IsRequired();

        Property(p => p.Name)
            .HasMaxLength(60)
            .IsRequired();

        HasRequired(p => p.Images).WithMany(); //???? Correct ??
        //HasMany(p => p.Images);  //???? Correct ??
    }
}

Error

The navigation property 'Images' is not a declared property on type 'Project'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The navigation property 'Images' is not a declared property on type 'Project'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

Source Error: 


Line 58:         public IQueryable<Project> GetAll(int pageIndex, int pageSize, params Expression<Func<Project, object>>[] includeProperties)
Line 59:         {
Line 60:             return includeProperties.Aggregate<Expression<Func<Project, object>>,
Line 61:                 IQueryable<Project>>(Context.Projects, (current, includeProperty) => current.Include(includeProperty)).OrderBy(p => p.Name).Skip(pageIndex).Take(pageSize);
Line 62:         }

The following code ProjectRepository.cs

public IQueryable<Project> GetAll(params Expression<Func<Project, object>>[] includeProperties)
{
    return includeProperties.Aggregate<Expression<Func<Project, object>>,
        IQueryable<Project>>(Context.Projects, (current, includeProperty) => current.Include(includeProperty));
}

public IQueryable<Project> GetAll(int pageIndex, int pageSize, params Expression<Func<Project, object>>[] includeProperties)
{
    return includeProperties.Aggregate<Expression<Func<Project, object>>,
        IQueryable<Project>>(Context.Projects, (current, includeProperty) => current.Include(includeProperty)).OrderBy(p => p.Name).Skip(pageIndex).Take(pageSize);
}

Solution

  • Mapping of collections of primitive types is not supported, they are not valid navigation properties. You will have to define an entity class and use it instead of string in the Images collection:

    public class Image
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Project
    {
        // ...
        public virtual ICollection<Image> Images { get; set; }
    }
    

    The new entity Image will be mapped to its own new table. You can then define a mapping in Fluent API:

    public ProjectConfiguration()
    {
        // ...
        HasMany(p => p.Images).WithRequired();
        // for a one-to-many relationship
        // it will also setup cascading delete
    }