1
votes

I'm having issues with a One To Many relationship using Fluent NHibernate. I've read the following question but I think my situation is simpler than that, so maybe I can avoid implementing IUserCollectionType.

I have a Project, and a Project has ProductBacklog. A ProductBacklog is a List of UserStory.

public class Project
{
    public virtual int Id { get; set; }

    public virtual ProductBacklog Backlog { get; protected set; }
}

public class ProductBacklog : List<UserStory>
{
}

When I try to map:

public class ProjectMapping : ClassMap<Project>
{
    public ProjectMapping()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        HasMany(x => x.Backlog).KeyColumn("ProjectId");
    }
}

I get the following error:

NHibernate.MappingException: Custom type does not implement UserCollectionType: ScrumBoard.Domain.ProductBacklog

If Project.Backlog is a List instead of being of type ProductBacklog, it will work:

public class Project
{
    public virtual int Id { get; set; }

    public virtual List<UserStory> Backlog { get; protected set; }
}

Is there any way to tell to Fluent NHibernate how to do the mapping without implementing IUserCollectionType?

Thanks in advance!

1
Hi @Matias! Did you solve your problem? I have a very similar issue. One of the suggested solutions is to use component mapping. Did that work for you?ironstone13

1 Answers

-4
votes

I think you are confusing the difference between a collection of records and a single record.

This is creating a reference to a single record of type ProductBacklog.
public virtual ProductBacklog Backlog { get; protected set; } 

Where this is creating a collection of records.
public virtual List<UserStory> Backlog { get; protected set; }

To do what you are trying, try the following:

public class Project
{
    public virtual int Id { get; set; }

    public virtual List<ProductBacklog> Backlog { get; set; }
}

public class ProductBacklog : UserStory
{
}

However, your best course of action may be the following which you already discovered:

public virtual List<UserStory> Backlog { get; set; }

Hopefully this helps.