【发布时间】:2015-10-08 12:06:22
【问题描述】:
问题的重新表述:
如何让代理解决部分加载的关节而不重新加载所有子关联?
上下文:
我有 2 个实体:
新闻
+ ID:整数
+ 日期:日期时间新闻翻译
+ NewsId:id(实体 News.Id 的外键)
+ 语言代码:char(2)
+ 标题:nvarchar(255)
+ 内容:nvarchar(2000)
一个News可以有多个NewsTranslation(每个LanguageCode一个)
例如,这是数据库中的数据:
News {
Id: 1,
Date: "01/01/2015"
Translations : [
{ NewsId: 1, LanguageCode: "en", Title: "Title", Contents: "English contents" },
{ NewsId: 1, LanguageCode: "fr", Title: "Titre", Contents: "French contents" }
]
}
我想加载新闻实体,但只加载当前语言翻译(例如英语“en”)。
我的代码:
News entity = null;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
using(var context = new MyContext())
{
// Disabling lazyloading (see why at the bottom of the code)
context.Configuration.LazyLoadingEnabled = false;
// Load News with inner join NewsTranslation to retrieve
// only english language "en"
entity = (from news in context.News
select new {
News = news,
NewsTranslation = from trans in news.NewsTranslation
where trans.NewsId == news.Id
&& trans.LanguageCode == "en"
select trans
}).First()
}
//
// Serialize traverse all properties and associations.
//
// WITHOUT above "context.Configuration.LazyLoadingEnabled = false;" it would
// lazy load all associations NewsTranslation. Additionaly, the Context is closed so
// an exception is thrown
//
serializer.Serialize(entity)
这段代码可以正常工作,但我很害怕这样做......
在不禁用 LazyLoading 配置的情况下有更好的方法吗?
更新:
基于 Brian Mains 的建议:
翻译第一法:
News entity = null;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
using(var context = new MyContext())
{
entity = (from trans in context.NewsTranslation
where trans.LanguageCode == "en"
select trans.News).First();
}
//
// The proxy try to lazy load News.NewsTransation association,
// so an exception is thrown beacause the Context is closed
//
//
serializer.Serialize(entity)
同样的问题,如何让代理解决部分加载的关节?
【问题讨论】:
-
当然:不要直接序列化实体/数据模型类。改用专用的“视图模型”或“域模型”......此外,该查询似乎从数据库中提取多个项目只是为了选择客户端的第一个项目。
标签: c# asp.net-mvc entity-framework linq