Silverlight 使用表示模型将rss数据插入wcf ria服务的数据库
我已经读了很多帖子,但是找不到我的答案。我的问题有点具体。在我的silverlight项目中,我想获得天气数据,比如温度,从yahoo weather获取状态和日期,并通过更改rss将其保存到我的数据库中。因此,我使用了webclient及其DownloadStringAsync和DownloadStringCompleted来获取数据。此外,我还在models FolderBea中的服务器中创建了一个演示模型,因为我想在我的服务中使用它,所以在DownloadStringCompleted事件处理程序i中我做过这样的事情:Silverlight 使用表示模型将rss数据插入wcf ria服务的数据库,silverlight,mvvm,wcf-ria-services,presentation-model,Silverlight,Mvvm,Wcf Ria Services,Presentation Model,我已经读了很多帖子,但是找不到我的答案。我的问题有点具体。在我的silverlight项目中,我想获得天气数据,比如温度,从yahoo weather获取状态和日期,并通过更改rss将其保存到我的数据库中。因此,我使用了webclient及其DownloadStringAsync和DownloadStringCompleted来获取数据。此外,我还在models FolderBea中的服务器中创建了一个演示模型,因为我想在我的服务中使用它,所以在DownloadStringCompleted事件
void xmlclient_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
{
XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";
if (e.Error == null)
{
XElement x = XElement.Parse(e.Result);
weatherquery1 =
from items in x.Descendants("item")
select new BusinessApplication1.Web.Models.WeatherConditionModel
{
PubDate = items.Element(yweather +"condition").Attribute("date").Value,
Status = items.Element(yweather + "condition").Attribute("text").Value
};
}
}
这是在我的viewmodel中,我测试了它的所有功能。我可以获取数据,也可以在datagrid或listbox中查看结果。
inow我想在数据库中保存数据。我想自动完成,而不是通过按钮或命令。例如,我想让它始终读取数据,并每隔5分钟将其保存到数据库中。因此,我创建了我的服务,并创建了一个自定义插入,我可以自己对其进行塑造:
private void MapwcModel(WeatherConditionTable wctable, WeatherConditionModel wcPM)
{
wctable.Status = wcPM.Status;
wctable.PubDate = wcPM.PubDate;
wctable.WeatherConditionID = wcPM.WeatherConditionID;
}
[Insert]
[Invoke]
public void InsertWeatherConditionData(WeatherConditionModel WeatherConditionData)
{
WeatherConditionTable wc = WeatherConditionContext.WeatherConditionTables.CreateObject();
MapwcModel(wc, WeatherConditionData);
wc.Status = WeatherConditionData.Status;
wc.PubDate = WeatherConditionData.PubDate;
WeatherConditionContext.WeatherConditionTables.AddObject(wc);
WeatherConditionContext.SaveChanges();
}
我的数据:
public IQueryable<WeatherConditionModel> GetWeatherConditionData()
{
return from p in this.WeatherConditionContext.WeatherConditionTables
select new WeatherConditionModel
{
WeatherConditionID = p.WeatherConditionID,
Status = p.Status,
PubDate = p.PubDate,
};
}
现在我不知道如何强制它保存数据。我在iewmodel中写了这个,但不起作用:
foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1)
{
WeatherConditionDomainContext context = new WeatherConditionDomainContext();
EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery();
context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery);
context.SubmitChanges(delegate(SubmitOperation operation)
{
if (operation.HasError)
{
operation.MarkErrorAsHandled();
}
}, null);
}
我不知道如何强制插入方法工作。有人请告诉我哪里错了?我知道有地方。给我指路。
致以最诚挚的问候我很难理解您要做什么,但您最后一步的模式应该更像:
// Create data context
WeatherConditionDomainContext context = new WeatherConditionDomainContext();
// Load existing entities
EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery();
context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery);
// Update or insert new entries
foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1)
{
// Update existing entries
// Or, add new entries if they did not exists
}
// Submit all changes (updates & inserts)
context.SubmitChanges(delegate(SubmitOperation operation)
{
if (operation.HasError)
{
operation.MarkErrorAsHandled();
}
}, null);
我找到了。非常感谢这本书:Silverlight 4 Unreleased- 第十三章-出自《大帝》:劳伦特·布尼翁 首先,不需要将WeatherConditionModel用作表示模型 需要在多个表中保存数据时使用的表示模型id。它只是一个用作 我的查询输出的持有者。 其次,根本不需要更改服务中的Insert方法,因为这里我只想将数据保存在一个表中 因此,只需在实体模型上创建服务并进行构建。以这种方式构建之后, 您可以在视图模型中调用您的表,但我不能,因为我更改了我的服务方法我更改了WeatherConditionTable 去上课!!如您所见,手动操作。 第三,在我的foreach循环中,我可以将数据保存到数据库中。 我有一个组合框、一个列表框和一个按钮,我选择我的城市 从combobox中,点击使用命令的按钮到我的GetRss 这项工作现在很好。它显示数据并将其保存到数据库中。 这是我的视图模型代码描述部分:
internal void GetRssFeed()
{
Feed selectedFeed = (Feed)FeedModel.FeedList[FeedModel.SelectedIndex];
FeedModel.SelectedFeedName = selectedFeed.FeedName;
WebClient xmlclient = new WebClient();
xmlclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlclient_DownloadStringCompleted);
xmlclient.DownloadStringAsync(new Uri(selectedFeed.FeedUrl));
}
WeatherConditionDomainContext context = new WeatherConditionDomainContext();
WeatherConditionTable wct = new WeatherConditionTable();
void xmlclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";
if (e.Error == null)
{
XElement x = XElement.Parse(e.Result);
weatherquery1 =
from items in x.Descendants("channel")
let item=items.Element("item")
select new WeatherConditionModel
{
Temp = Convert.ToInt32(item.Element(yweather + "condition").Attribute("temp").Value),
PubDate = item.Element(yweather + "condition").Attribute("date").Value,
Status = item.Element(yweather + "condition").Attribute("text").Value,
Humidity=Convert.ToInt32(items.Element(yweather + "atmosphere").Attribute("humidity").Value)
};
foreach (WeatherConditionModel wc in weatherquery1)
{
wct.Temp = wc.Temp;
wct.Status = wc.Status;
wct.PubDate = DateTime.Now.ToShortTimeString();
wct.Humidity = wc.Humidity;
context.WeatherConditionTables.Add(wct);
context.SubmitChanges();
}
}
else
{
MessageBox.Show(e.Error.Message);
}
}
谢谢大家的关注。我希望这对某人有所帮助。如果有人有更好的想法,请告诉我。
如果有帮助,请标记为答案。您可以通过创建WeatherConditionDomainContext添加更改,最好只创建一次。。。圈外!并将条目添加到上下文表中。到目前为止,您的保存代码不会加载和提交更改,但实际上没有将任何更改添加到上下文的表中,因此不会保存任何内容。谢谢。但首先,我并没有告诉它在哪个时间间隔内保存更改,所以我希望它至少保存一个数据以供测试,但数据库是空的;其次,我在上下文表中添加了条目,您是指在视图模型中?您能用代码显示我吗?第三,我的插入内容格式正确吗?谢谢没有人能帮忙吗?我不知道该怎么办?关于这一部分:/在weatherquery1中更新或插入每个BusinessApplication1.Web.Models.WeatherConditionModel el的新条目{//更新现有条目//,或者,如果它们不存在,则添加新条目}我该怎么做?如何在此处插入?可以通过向数据上下文中的相应表添加对象来执行插入。这是基本的实体框架,而不是特定于Silverlight的。最好阅读EF。谢谢。但在我的视图模型中,我没有访问表名或实体的权限。我无法写入:Mytable t=new Mytable;T、 列=某物;。。。。。。context.Mytable.Addt;上下文,提交更改;在codebehind中你可以,但我希望它在mvvm中。上面的代码不必在VM中。虽然您的虚拟机应该只包含特定形状的数据而不知道数据来自何处是正确的,但是虚拟机通常由某个地方的代码填充,最好是在控制器IMHO中。用于反向存储任何更改的代码属于同一位置。