Entity framework 如何通过Fluent API更改数据库列名

Entity framework 如何通过Fluent API更改数据库列名,entity-framework,ef-fluent-api,Entity Framework,Ef Fluent Api,我有以下代码: public abstract class Entity { public virtual int Id { get; set; } } public class Category : Entity { public string Name { get; set; } public virtual ICollection<Category> Children { get; set; } public virtual ICollecti

我有以下代码:

public abstract class Entity
{
    public virtual int Id { get; set; }
}

public class Category : Entity
{
    public string Name { get; set; }
    public virtual ICollection<Category> Children { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

public class Item : Entity
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public DateTime DateCreated { get; set; }
    public virtual Category Category { get; set; }
}
公共抽象类实体
{
公共虚拟整数Id{get;set;}
}
公共类类别:实体
{
公共字符串名称{get;set;}
公共虚拟ICollection子项{get;set;}
公共虚拟ICollection项{get;set;}
}
公共类项:实体
{
公共字符串名称{get;set;}
公共十进制价格{get;set;}
public DateTime DateCreated{get;set;}
公共虚拟类别{get;set;}
}
这种语法

modelBuilder.Entity<Category>().Property(x => x.Children).HasColumnName("CategoryID");
modelBuilder.Entity().Property(x=>x.Children.HasColumnName(“CategoryID”);
提供项目文件行的严重性代码说明 错误CS0453类型“ICollection”必须是不可为null的值类型,才能将其用作泛型类型或方法“StructuralTypeConfiguration.Property(Expression>)”中的参数“T”

问:如何通过Fluent API更改数据库列名?

集合本身无法映射到一列。您应该将其映射为一对多关联:

modelBuilder.Entity<Category>()
            .HasMany(c => c.Children)
            .WithOptional()
            .Map(m => m.MapKey("CategoryID"));
modelBuilder.Entity()
.HasMany(c=>c.Children)
.WithOptional()
.Map(m=>m.MapKey(“CategoryID”);