【问题标题】:Mapping ICollection <string> using Fluent API使用 Fluent API 映射 ICollection <string>
【发布时间】:2011-11-08 22:04:37
【问题描述】:

FluentAPI 无法根据以下代码创建关系模型:

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 ??
    }
}

错误

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:         }

以下代码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);
}

【问题讨论】:

    标签: asp.net-mvc-3 entity-framework-4.1 ef-code-first fluent-interface


    【解决方案1】:

    不支持原始类型集合的映射,它们不是有效的导航属性。您必须定义一个实体类并在 Images 集合中使用它而不是 string

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

    新实体Image 将映射到它自己的新表。然后您可以在 Fluent API 中定义映射:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2012-01-02
      • 2013-12-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2011-06-29
      相关资源
      最近更新 更多