【发布时间】: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