Asp.net mvc 3 映射ICollection<;字符串>;使用fluentapi
FluentAPI无法基于以下代码创建关系模型:Asp.net mvc 3 映射ICollection<;字符串>;使用fluentapi,asp.net-mvc-3,entity-framework-4.1,ef-code-first,fluent-interface,Asp.net Mvc 3,Entity Framework 4.1,Ef Code First,Fluent Interface,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 : EntityTypeConfigura
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 ??
}
}
不支持基元类型集合的映射,它们不是有效的导航属性。您必须定义实体类并使用它,而不是在
图像
集合中使用字符串
:
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Project
{
// ...
public virtual ICollection<Image> Images { get; set; }
}
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);
}
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Project
{
// ...
public virtual ICollection<Image> Images { get; set; }
}
public ProjectConfiguration()
{
// ...
HasMany(p => p.Images).WithRequired();
// for a one-to-many relationship
// it will also setup cascading delete
}