Tomek
Tomek

Reputation: 63

NHibernate mappings

Could anyone help me with this? I've two classes in my project: User and Article. It looks like this: enter image description here

    public class User
    {
        public virtual int UserID { set; get; }
        public virtual string Name { set; get; }
        public virtual IList<Article> Articles { set; get; }
        public User() { }
    }   

    public class Article
    {
        public virtual int ArticleID { set; get; }
        public virtual string Title { set; get; }
        public virtual string Body { set; get; }
        public virtual User User { set; get; }
        public Article() { }
    }

Now, I would like that tables in the database looked like this: enter image description here

I have use XML mapping. Please help me.

Upvotes: 0

Views: 95

Answers (3)

Rippo
Rippo

Reputation: 22424

As Sly rightly states you can move the UserId to the Article table

If the user is deleted then set the UserId to a special user, e.g. A System User This system user then can own any articles that no longer have any users assigned thus keeping integrity.

Upvotes: 1

Guclu Ozturk
Guclu Ozturk

Reputation: 152

The given ER diagram states any given article might have more than one author (user). In your case you still don't need that schema.

Considering the deletion issue, if you plan to delete any User row from the database, you will end up with multiple articles with some user ID's whose references are dangling thus yielding useless user information. If you only need article info, just create a 1-to-n relation with user and article. Otherwise (If you are going to use article info with it's user even the user get deleted) I would suggest holding a deleted flag instead of deleting the entry.

Upvotes: 1

Sly
Sly

Reputation: 15217

Your DB schema shows many-to-many relation, while entities are in many-to-one. So you should decide what kind of relation do you need. Additional info about mappings you can find here

Upvotes: 2

Related Questions