1
votes

I am fairly new to NHibernate and the .NET framework.

I want to map my Project class, which has one-to-many relations Subproject and ProjectTask classes.

public class Project : ProjectItem
{
    public virtual IList<Subproject> Subprojects { get; set; }

    public virtual IList<ProjectTask> Tasks { get; set; }

    [NotNull]
    public virtual ProjectStatus Status { get; set; }

    public Project()
    {
        Subprojects = new List<Subproject>();
        Tasks = new List<ProjectTask>();
        Status = new ProjectStatus();
    }

    public virtual void AddSubProject(Subproject subproject)
    {
        subproject.Project = this;
        Subprojects.Add(subproject);
    }

    public virtual void RemoveSubProject(Subproject subproject)
    {
        Subprojects.Remove(subproject);
    }

    public virtual void AddTask(ProjectTask task)
    {
        task.Project = this;
        Tasks.Add(task);
    }

    public virtual void RemoveTask(ProjectTask task)
    {
        Tasks.Remove(task);
    }
}

public class ProjectTask : ProjectItemTask
{
    [NotNull]
    public virtual Project Project { get; set; }
}

public class Subproject : ProjectItem
{
    [NotNull]
    public virtual Project Project { get; set; }
}

My entities inherit the Id property from an abstract base class.

I use automapping to overrides to re-define my mappings:

public class ProjectMappingOverride : IAutoMappingOverride<Project>
{
    public void Override(AutoMapping<Project> mapping)
    {
        mapping.HasMany<Subproject>(x => x.Subprojects)
            .Cascade.All()
            .Inverse()
            .Not.LazyLoad()
            .AsBag();

        mapping.HasMany<ProjectTask>(x => x.Tasks)
            .Cascade.All()
            .Inverse()
            .Not.LazyLoad()
            .AsBag();

        mapping.HasOne<ProjectStatus>(x => x.Status)
            .Cascade.All()
            .Not.LazyLoad();
    }
}

public class SubprojectMappingOverride : IAutoMappingOverride<Subproject>
{
    public void Override(AutoMapping<Subproject> mapping)
    {
        mapping.References<Project>(x => x.Project)
            .ForeignKey("ProjectId");
    }
}

public class ProjectTaskMappingOverride : IAutoMappingOverride<ProjectTask>
{
    public void Override(AutoMapping<ProjectTask> mapping)
    {
        mapping.References<Project>(x => x.Project)
            .ForeignKey("ProjectId");
    }
}

When I only map the association with Subproject class everything works fine and all instances of Subproject class in Project.Subprojects will be stored to database correctly. When I use the second override for ProjectTask class, I get the following exception:

---> NHibernate.HibernateException: There is already an object named 'ProjectId' in the database. Could not create constraint. See previous errors.

---> System.Data.SqlClient.SqlException: There is already an object named 'ProjectId' in the database.

Does anybody know, why this happens?

1

1 Answers

3
votes

You should map your Project references with a Column and not a ForeignKey

mapping.References<Project>(x => x.Project)
            .Column("ProjectId");