0

I have a following code:

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

This syntax

modelBuilder.Entity<Category>().Property(x => x.Children).HasColumnName("CategoryID");

gives me Severity Code Description Project File Line Error CS0453 The type 'ICollection' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'StructuralTypeConfiguration.Property(Expression>)'

Q: How I can change database column name via Fluent API?

user2457382
  • 329
  • 4
  • 14

1 Answers1

0

The collection itself can't be mapped to one column. You should map this as a one-to-many association:

modelBuilder.Entity<Category>()
            .HasMany(c => c.Children)
            .WithOptional()
            .Map(m => m.MapKey("CategoryID"));
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291