c# - Why fluent nhibernate automapping creates many foreign keys instead of one? -
i use fluent nhibernate , 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 don't understand why fnh creates wrong schema on simpliest entities. that's fnh creates in db:
a busy cat http://i008.radikal.ru/1504/4b/25dcc9148f7e.png
i can't understand why fnh creates 2 references (author_id , user_id) user table instead of single reference (only author_id).
i found workaround here fluent nhibernate automapping -- 2 foreign keys same table? , here fluent nhibernate automappings generating 2 foreign keys 1 relationship don't want use because don't understand why should set every thing hands if use automappings should work me (at-least simpliest , obvious mappings in entities).
you have document entity referring user entity (0-1 relationship) through property named author, in same time, in user entity refer document in one-to-many relationship.
fluent nhibernate automapping works conventions, , specific hasmanyconvention maps relationship creating foreign key name based on name (and not type) of referring entity (in case user)
so nhibernate, when creating relationship between user , document, creates user_id key in document table. correct convention behavior.
Comments
Post a Comment