How to do check in ApplicationDataServices?
本文关键字:ApplicationDataServices in check to do How | 更新日期: 2023-09-27 18:19:18
我正在制作一个简单的电灯开关应用程序。我有一个包含用户提交信息的报告实体。在这个实体中,用户从另一个表中选择严重性级别(有3个级别-轻微,中等和严重)。
现在,我需要灯开关在有人提交临界水平的报告时发送电子邮件。我知道我需要从服务器端发送电子邮件,所以我把我的sendEmail方法放到了插入方法中。它位于applicationdataservice . lsmall .cs中,如下所示:
partial void Report_Inserted(IssueList entity)
{
MailMessage mm = new MailMessage("myemail", "targetEmail");
mm.Subject = "Subject";
mm.Body = "Body";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.office365.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "myUserName";
NetworkCred.Password = "Password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
// }
}
电子邮件方法是有效的,问题是我不知道如何检查报告级别是否重要。它应该看起来像
if(level == LevelsSet.Where(e => e.Level == "Critical").FirstOrDefault()){
sendEmailMethod
}
但它不起作用。Lightswitch似乎不允许我从这里访问关卡数据。你知道如何绕过这个或在哪里移动我的senemail方法吗?
我个人会将电子邮件处理程序添加到Report_saving()处理程序中。
public partial class ApplicationDataService
{
partial void SaveChanges_Executing()
{
var changes = this.DataWorkspace.ApplicationData.Details.GetChanges();
var inserted = changes.AddedEntities;
foreach (var entity in inserted)
{
try
{
var report = (Report)entity;
if (report.Level.LevelName == "Critical")
{
sendNotification(report);
}
}
catch (InvalidCastException) {}
// Do nothing - this is just a simple way to filter the entity types
// in the EntityChangeSet
}
}
void sendNotification(Report report) {
MailMessage mm = new MailMessage("myemail", "targetEmail");
mm.Subject = "Critical Report submitted!";
mm.Body = report.ToString();
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.office365.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "myUserName";
NetworkCred.Password = "Password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
}
}
这将在提交时快速扫描任何新的Report实体。您不一定希望在用户完成插入时触发此消息,因为他们可能不小心选择了错误的Level。如果添加的报告实体对应的LevelName字段为"Critical",则触发该事件。
HTH