Lethargic Coder
Lethargic Coder

Reputation: 15

Why is schemaexport generating 2 foreign keys?

I'm using fluentnhibernate on SQL Server 2008.

I have to objects Staff

public class Staff : BaseEntity
{
    public virtual string UserName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Email { get; set; }
    public virtual bool Active { get; set; }
    public virtual StaffRole StaffRole { get; set; }
}

and StaffRole

public class StaffRole : BaseEntity
{
    public virtual int RoleId { get; set; }
    public virtual int RoleName { get; set; }
    public virtual IList<Staff> Staffs { get; set; }
}

and mapped them accordingly:

public class StaffRoleMap : ClassMap<StaffRole>
{
    public StaffRoleMap()
    {
        Id(x => x.RoleId).Not.Nullable().Length(15);
        Map(x => x.RoleName).Not.Nullable().Length(75);
        HasMany(x => x.Staffs);
    }
}
public class StaffMap : ClassMap<Staff>
{
    public StaffMap()
    {
        Id(x => x.UserName).Not.Nullable().Length(15);
        Map(x => x.FirstName).Not.Nullable().Length(25);
        Map(x => x.LastName).Not.Nullable().Length(25);
        Map(x => x.Email).Not.Nullable().Length(30);
        Map(x => x.Active).Not.Nullable().Default("1");
        References(x => x.StaffRole)
            .ForeignKey("FKStaffRole")
            .Column("RoleId")
            .Cascade.None()
            .Not.Nullable()
            ;
    }
}

When i call schemaexport, why is it generating 2 foreign keys for the StaffRole? I always get RoleId and StaffRole_id. RoleId is correct since it is not nullable, but the extra column/fk is nullable.

Am I missing something here?

Upvotes: 1

Views: 99

Answers (1)

Adam Dymitruk
Adam Dymitruk

Reputation: 129762

Yes. You must set "inverse" on one side of the relationship. This would be equivalent to setting inverse = true in regular nhibernate xml config.

Upvotes: 1

Related Questions