有什么不对的linqTOsql自参照对象映射? [英] What's wrong with this linqTOsql self referencing object mapping?
问题描述
我试图创建一个使用linqTOsql映射自参考对象。到目前为止,我绝对在我头上。这里的code我有:
I'm trying to create a self referencing object using linqTOsql mapping. So far, I am definitely in over my head. Here's the code I have:
[Table]
public class Category
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 catID { get; set; }
public Int64 parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
internal EntityRef<IEnumerable<Category>> _category;
[Association(ThisKey = "parentCatID", Storage = "_category")]
public IEnumerable<Category> category {
get { return _category.Entity; }
set { _category.Entity = value; }
}
}
我的 fakeRepository
是这样定义的:
// Fake hardcoded list of categories
private static IQueryable<Category> fakeCategories = new List<Category> {
new Category { catID = 1, parentCatID = 0, catName = "Root", catDescription = "" },
new Category { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
new Category { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
new Category { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
new Category { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
new Category { catID = 6, parentCatID = 0, catName = "Another Root", catDescription = "" },
new Category { catID = 7, parentCatID = 0, catName = "Ze German Root", catDescription = "" },
new Category { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
new Category { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();
我通过类别
来这样的观点:
public ActionResult CategoryTree()
{
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID == 0)
.OrderBy(x => x.catName);
return View(cats);
}
这是我遇到的问题是,这一切编译,但我没有得到任何超出根类别。 模型[0] .category
终止返回null。
The problem that I'm running into is that all of this compiles, but I don't get anything beyond the root categories. Model[0].category
is returning null.
什么是错我的自我参照的对象?
What is wrong with my self-referencing object?
我不知道,如果它不工作,因为我没有在我的 fakeRepository
一个真正的LINQ到SQL数据上下文。如果是这样的话,有没有办法解决?我能得到这个没有到数据库的连接工作?
I wonder if it's not working because I don't have a real linq-to-sql data context in my fakeRepository
. If that's the case, is there a way around that? Can I can get this to work without a connection to a database?
推荐答案
是啊,你击中了要害。它不工作,因为你使用的是伪造的资料库。
Yeah, you hit the nail on the head. It's not working because you're using a fake repository.
LINQ到SQL将所有的接线为你设置基于性能相关的集合(安培;它们的属性)。你安装在模型
Linq-to-Sql does all the wiring up for you and sets the related collections based on the properties (& their attributes) that you setup in your model.
我不知道如何在不与数据库的连接,做到这一点,因为内部的EntityRef&LT;&IEnumerable的LT;类别&GT;&GT; _category;
是完全陌生的我 - 我更男人的POCO模型类
I don't know how to accomplish this without a connection to the database because internal EntityRef<IEnumerable<Category>> _category;
is completely foreign to me - I'm more of a POCO model type of guy.
快速谷歌之后,我发现这一点 - 如何:映射数据库关系(的LINQ to SQL)
After a quick google, I found this - How to: Map Database Relationships (LINQ to SQL)
你可以改变你的模型如下:
Could you change your model to read:
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 CatId { get; set; }
[Column]
public Int64 ParentCatId { get; set; }
[Column]
public string CatName { get; set; }
[Column]
public string CatDescription { get; set; }
private EntitySet<Category> _ChildCategories;
[Association(Storage = "_ChildCategories", OtherKey = "ParentCatId")]
public EntitySet<Category> ChildCategories
{
get { return this._ChildCategories; }
set { this._ChildCategories.Assign(value); }
}
private EntityRef<Category> _ParentCategory;
[Association(Storage = "_ParentCategory", ThisKey = "ParentCatId")]
public Category ParentCategory
{
get { return this._ParentCategory.Entity; }
set { this._ParentCategory.Entity = value; }
}
现在,因为你的 ChildCategories
的类型为的EntitySet&LT;类别&GT;
(从继承IList的&LT; T&GT;
),你应该能够连线假关系了自己
Now because your ChildCategories
is of type EntitySet<Category>
(which inherits from IList<T>
) you should be able to wire fake relationships up yourself.
所以你可以做这样的事情:
So you could do something like this:
private static IQueryable<Category> GetFakeCategories()
{
var categories = new List<Category> {
new Category { CatId = 1, ParentCatId = 0, CatName = "Root", CatDescription = "" },
new Category { CatId = 2, ParentCatId = 1, CatName = "Category w/subs", CatDescription = "" },
//Blah
new Category { CatId = 8, ParentCatId = 3, CatName = "Brand new cats", CatDescription = "" },
new Category { CatId = 9, ParentCatId = 8, CatName = "Brand new cats sub", CatDescription = "" }
};
//Loop over the categories to fake the relationships
foreach (var category in categories)
{
category.ChildCategories = new EntitySet<Category>(); //new up the collection
foreach (var subLoopCategory in categories)
{
if (category.ParentCatId == subLoopCategory.CatId)
category.ParentCategory = subLoopCategory;
if (category.Id == subLoopCategory.ParentCatId)
category.ChildCategories.Add(subLoopCategory);
}
}
return categoies.AsQueryable();
}
它工作在我的头上,至少...: - )
It works in my head at least... :-)
HTHS,结果
查尔斯
HTHs,
Charles
编辑:回复:下面的评论关于空引用 _childCategories
Re: Comment below about a null reference on _childCategories
.
您可以改变模型如下:
private EntitySet<Category> _ChildCategories = new EntitySet<Category>();
这篇关于有什么不对的linqTOsql自参照对象映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!