将NHibernate唯一键约束放在一对多相关实体上

时间:2016-03-25 19:36:22

标签: c# nhibernate mapping-by-code

我有这种类

public class Project
{
    public virtual int ProjectId { get; protected set; }
    public virtual ISet<ProjectMembership> ProjectMemberships { get; protected set; }
}

这是ProjectMembership

public class ProjectMembership
{
    public virtual int MembershipId { get; protected set; }
    public virtual int DeveloperId { get; protected set; }
    public virtual string Role { get; protected set; }
}

这是项目的映射:

public class ProjectMap : ClassMapping<Project>
{
    public ProjectMap()
    {
        Id(project => project.ProjectId, mapper => mapper.Generator(Generators.Identity));
        Set(
            membership => membership.ProjectMemberships,
            mapper =>
            {
                mapper.Cascade(Cascade.All);
                mapper.Key(m => m.Column("ProjectId"));
            },
            action => action.OneToMany());
    }
}

这是成员资格:

public class ProjectMembershipMap : ClassMapping<ProjectMembership>
{
    public ProjectMembershipMap()
    {
        Id(project => project.MembershipId, mapper => mapper.Generator(Generators.Identity));
        Property(project => project.DeveloperId);
        Property(project => project.Role);
    }
}

问题是,如何在“ProjectId”和“DeveloperId”列上设置约束,以防止在单个项目上获得两个相同的开发人员的可能性? 我已阅读有关UniqueKey的内容,但我不知道如何在“ProjectId”列中使用它。

0 个答案:

没有答案