0
votes

I use Fluent Nhibernate and have 2 entities:

public class Document
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
    public virtual User Author { get; set; }
    public virtual DateTime Date { get; set; }
}

and

public class User
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
    public virtual IList<Document> Docs { get; set; }
    public User()
    {
        Docs = new List<Document>();
    }
}

and I don't understand why fnh creates that wrong schema on this simpliest entities. That's what fnh creates in my db:

a busy cat

I can't understand why fnh creates 2 references (Author_id and User_id) to User table instead of a single reference (only Author_id).

I found an workaround here Fluent Nhibernate AutoMapping -- 2 foreign keys to same table? and here Fluent NHibernate Automappings generating 2 foreign keys for 1 relationship but I don't want to use it because I don't understand why I should set up every thing by my hands if I use automappings that should do all work for me (at-least that simpliest and obvious mappings as in my entities).

1

1 Answers

0
votes

You have a Document entity referring a User entity (0-1 relationship) through a property named Author, but in the same time, in User entity you refer Document in a one-to-many relationship.

Fluent NHibernate automapping works with conventions, and the specific HasManyConvention maps the relationship creating a foreign key name based on the NAME (and not the type) of the referring entity (in this case USER)

So NHibernate, when creating the relationship between User and Document, creates a User_Id key in the Document table. This is a correct convention behavior.