使用实体框架和Linq 使用 DomainCollectionView 检索插入的 ID
我在 Silverlight 4.0 中使用实体框架。当用户在我的表单上输入关键字时,我会检查我的 EntityList
看看该关键字是否存在。如果没有,我将其添加到数据库中&刷新我的实体列表。我不确定我的做法是否 100% 符合最佳实践。
我正在使用 MVVM 友好的 DomainDataSource,DataCollectionView,感谢 凯尔·麦克莱伦。这是我在构造函数中加载关键字实体的代码(上面有声明):
private readonly MI2DomainContext _context = new MI2DomainContext();
private readonly DomainCollectionView<Keyword> _keywordView;
private readonly DomainCollectionViewLoader<Keyword> _keywordLoader;
private readonly EntityList<Keyword> _keywordSource;
private int enteredKeywordID = 0;
public ProvisionDashboardViewModel()
{
this._keywordSource = new EntityList<Keyword>(this._context.Keywords);
this._keywordLoader = new DomainCollectionViewLoader<Keyword>(LoadKeywords, LoadKeywordsCompleted);
this._keywordView = new DomainCollectionView<Keyword>(this._keywordLoader, this._keywordSource);
this._keywordView.MoveToFirstPage();
}
用户输入关键字并单击“搜索”后,我使用命令调用包含此代码的方法,其中“searchText”包含用户输入的值去寻找。这将创建我的 Keyword 类的一个新实例,将其添加到我的上下文中,提交更改,包括完成后的回调函数:
enteredKeywordID = (int)(from d in this._keywordSource.Source where d.keyword.ToLower() == searchText.ToLower() select d.keywordID).FirstOrDefault();
if (0 == enteredKeywordID) {
Keyword kw = new Keyword() { keyword = searchText };
this._context.Keywords.Add(kw);
this._context.SubmitChanges(KeywordsAddedCompleted, null);
}
回调函数循环访问AddedEntities 集合(为1)并设置enteredKeywordID 值。然后,它使用 MVVM-light 消息传递发送一条消息,以调用使用之前输入的关键字搜索的 API。此 API 返回稍后保存到我的数据库的数据,需要在此处设置 EnteredKeywordID 变量:
private void KeywordsAddedCompleted(SubmitOperation so)
{
if (so.HasError)
{
so.MarkErrorAsHandled();
}
else
{
foreach (Keyword item in so.ChangeSet.AddedEntities) {
enteredKeywordID = item.keywordID;
}
Messenger.Default.Send<string>(SearchText, "GenerateKeywords");
}
}
是否有更好的方法来执行此操作?
I'm using Entity Framework in Silverlight 4.0. When a user enters a keyword on my form, I check my EntityList<Keyword>
see if the keyword exists or not. If not, I add it to the database & refresh my EntityList. I'm not sure if the way I'm doing it is 100% on track with best practice.
I'm using an MVVM-Friendly DomainDataSource, the DataCollectionView, thanks to Kyle McClellan. Here is my code for loading my keywords entity, in my constructor (with declarations above):
private readonly MI2DomainContext _context = new MI2DomainContext();
private readonly DomainCollectionView<Keyword> _keywordView;
private readonly DomainCollectionViewLoader<Keyword> _keywordLoader;
private readonly EntityList<Keyword> _keywordSource;
private int enteredKeywordID = 0;
public ProvisionDashboardViewModel()
{
this._keywordSource = new EntityList<Keyword>(this._context.Keywords);
this._keywordLoader = new DomainCollectionViewLoader<Keyword>(LoadKeywords, LoadKeywordsCompleted);
this._keywordView = new DomainCollectionView<Keyword>(this._keywordLoader, this._keywordSource);
this._keywordView.MoveToFirstPage();
}
After a user enters a keyword and clicks Search, I use commanding to call a method containing this code, where 'searchText' contains the user-entered value to search for. This creates a new instance of my Keyword class, adds it to my context, submits the changes, including the callback function upon completion:
enteredKeywordID = (int)(from d in this._keywordSource.Source where d.keyword.ToLower() == searchText.ToLower() select d.keywordID).FirstOrDefault();
if (0 == enteredKeywordID) {
Keyword kw = new Keyword() { keyword = searchText };
this._context.Keywords.Add(kw);
this._context.SubmitChanges(KeywordsAddedCompleted, null);
}
The callback function iterates through the AddedEntities collection (of 1) and sets the enteredKeywordID value. It then sends out a message using MVVM-light messaging to call an API that uses the keyword search entered earlier. This API returns data that is later saved to my database, requiring the enteredKeywordID variable set here:
private void KeywordsAddedCompleted(SubmitOperation so)
{
if (so.HasError)
{
so.MarkErrorAsHandled();
}
else
{
foreach (Keyword item in so.ChangeSet.AddedEntities) {
enteredKeywordID = item.keywordID;
}
Messenger.Default.Send<string>(SearchText, "GenerateKeywords");
}
}
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论