I have two classes:
public class Catalog
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Article>? Articles { get; set; }
public virtual IList<Catalog>? SubCatalogs { get; set; }
}
and
public class Article
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual string Text { get; set; }
public virtual Catalog? Catalog { get; set; }
}
I write this mappings:
public class CatalogMap : ClassMap<Catalog>
{
public CatalogMap()
{
Table("Catalogs"); // Указываем имя таблицы в базе данных
Id(x => x.Id); // Указываем, что поле Id является первичным ключом
Map(x => x.Name); // Маппинг свойства Name как обычное поле в базе данных
HasMany(x => x.Articles) // Определяем отношение один-ко-многим с сущностью Article
.KeyNullable()
.Cascade.All() // Указываем каскадирование операций
.Inverse() // Указываем, что связь управляется сущностью Article
.KeyColumn("CatalogId"); // Указываем имя внешнего ключа в таблице Articles
HasMany(x => x.SubCatalogs) // Определяем отношение один-ко-многим с сущностью Catalog
.KeyNullable()
.Cascade.All() // Указываем каскадирование операций
.Inverse() // Указываем, что связь управляется сущностью Catalog
.KeyColumn("ParentCatalogId"); // Указываем имя внешнего ключа в таблице Catalogs
}
}
and
public class ArticleMap : ClassMap<Article>
{
public ArticleMap()
{
Table("Articles"); // Указываем имя таблицы в базе данных
Id(x => x.Id); // Указываем, что поле Id является первичным ключом
Map(x => x.Title); // Маппинг свойства Title как обычное поле в базе данных
Map(x => x.Text); // Маппинг свойства Text как обычное поле в базе данных
References(x => x.Catalog) // Определяем отношение многие-к-одному с сущностью Catalog
.Column("CatalogId") // Указываем имя внешнего ключа в таблице Articles
.Nullable()
.Cascade.All(); // Указываем каскадирование операций
}
}
I need to get all Catalogs including all Articles and SubCatalogs. I have already tried many options, but unfortunately nothing helped. Here is my code for creating a Catalog with the specified Articles and SubCatalogs:
var session = _sessionFactory.OpenSession();
using (var transaction = session.BeginTransaction())
{
var cat = new Catalog
{
Name = "New Catalog"
};
var article1 = new Article
{
Title = "Article 1",
Text = "Text 1",
Catalog = cat
};
var article2 = new Article
{
Title = "Article 2",
Text = "Text 2",
Catalog = cat
};
var subCatalog1 = new Catalog
{
Name = "Sub Catalog 1"
};
var subCatalog2 = new Catalog
{
Name = "Sub Catalog 2"
};
cat.Articles = new List<Article> { article1, article2 };
cat.SubCatalogs = new List<Catalog> { subCatalog1, subCatalog2 };
session.Save(cat);
transaction.Commit();
}
And here is my code to get all Catalogs:
var session = _sessionFactory.OpenSession();
using (ITransaction transaction = session.BeginTransaction())
{
var catalogs = session.QueryOver<Catalog>()
.Fetch(x => x.Articles).Eager
.Fetch(x => x.SubCatalogs).Eager
.List();
var res = catalogs.ToList();
return res;
}
I tried writing a query in SQL form, but that didn't work either. Every time I get a different Exception.