2012-12-26 23 views
1

我遇到了使用Fluent NHibernate一對多關係的問題。我讀過以下question,但我認爲我的情況比這更簡單,所以也許我可以避免實現IUserCollectionType。從列表繼承的類的流利的NHibernate映射「自定義類型不實現UserCollectionType」

我有一個項目,一個項目有ProductBacklog。 ProductBacklog是UserStory的列表。

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

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

public class ProductBacklog : List<UserStory> 
{ 
} 

當我嘗試映射:

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

我得到以下錯誤:

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

如果Project.Backlog是一個列表而不是類型ProductBacklog的,它會工作:

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

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

有沒有辦法告訴Fluent NHibernate如何在不實現IUserCollectionType的情況下做映射?

在此先感謝!

+0

Hi @Matias!你解決了你的問題嗎?我有一個非常類似的問題。建議的解決方案之一是使用組件映射。這對你有用嗎? – ironstone13

回答

-2

我認爲你在混淆記錄集合和單個記錄之間的區別。

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

要做到你想什麼,請嘗試以下操作:

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

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

public class ProductBacklog : UserStory 
{ 
} 

然而,最好的行動路線可能是你已經發現了以下情況:

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

希望這幫助。

相關問題