Many to many relations with ServiceStack.OrmLite

孤者浪人 提交于 2019-12-29 04:58:05

问题


I've been checking ServiceStack's documentation, but I haven't found a way to do many to many relationships with ServiceStack.OrmLite, is it supported? Is there a workaround (without writing raw sql)?

I'd like to have something like this:

Article <- ArticleToTag -> Tag

Thanks!!


回答1:


It's not implicitly handled automatically for you behind the scenes if that's what you mean? But as OrmLite is just a thin wrapper around ADO.NET interfaces anything is possible.

In OrmLite, by default every POCO maps 1:1 with a table. So if you wanted the table layout you would create it just as it looks in your database, e.g.

var article = new Article { ... };
var tag = new Tag { ... };
var articleTag = new ArticleTag { ArticleId = article.Id, TagId = tag.Id };

db.Insert(article, tag, articleTag);

Although you might want to take advantage of the built-in blobbing in OrmLite where any complex type just gets serialized and stored in a single text field. So you could do something like:

var article = { new Article { Tags = { "A","B","C" } };

Where Tags is just a List<string> and OrmLite will take care of transparently serializing it in the database field for you.



来源:https://stackoverflow.com/questions/12539440/many-to-many-relations-with-servicestack-ormlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!