与 ServiceStack.OrmLite 的多对多关系 [英] Many to many relations with ServiceStack.OrmLite
问题描述
我一直在查看 ServiceStack 的文档,但我还没有找到与 ServiceStack.OrmLite 建立多对多关系的方法,是否支持?是否有解决方法(无需编写原始 sql)?
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)?
我想要这样的东西:
文章 <- ArticleToTag -> 标签
Article <- ArticleToTag -> Tag
谢谢!!
推荐答案
如果这就是您的意思,它不会在幕后自动为您自动处理?但由于 OrmLite 只是 ADO.NET 接口的一个薄包装器,一切皆有可能.
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.
在 OrmLite 中,默认情况下每个 POCO 都将 1:1 映射到一个表.因此,如果您想要表格布局,您可以按照它在数据库中的外观创建它,例如
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);
尽管您可能希望利用 OrmLite 中的内置 blob,其中任何复杂类型都可以序列化并存储在单个文本字段中.所以你可以这样做:
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" } };
其中 Tags 只是一个 List
,OrmLite 会为您在数据库字段中透明地序列化它.
Where Tags is just a List<string>
and OrmLite will take care of transparently serializing it in the database field for you.
这篇关于与 ServiceStack.OrmLite 的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!