0

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).

Community
  • 1
  • 1

1 Answers1

0

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.

Hellraiser
  • 579
  • 7
  • 18
  • **you're mapping a one-to-many relationship as well as a 0 to 1 relationship on the other side** -- don't understand this. I have class Document with User Author and class User with list of Documents. I think it's 1 to many for User and many to 1 for Document. So, I don't understand why you said I have 0 to 1 and 1 to 1. – user3649516 May 23 '15 at 05:30
  • I modified the answer. Hope it helps – Hellraiser May 26 '15 at 10:18