//构造函数 public AttentionService() { Boolean same = false; using (var dbcontext = new AttentionContext()) { this.Attentions = dbcontext.Attentions.ToList(); } this.MatchType = "Approximate"; this.Listeners = new List <ListenerInfo>(); foreach (Attention att in Attentions) { same = false; foreach (ListenerInfo listenerInfo in Listeners) { if (listenerInfo.Listener.Equals(att.Listener)) { same = true; listenerInfo.Count++; break; } } if (!same) { this.Listeners.Add(new ListenerInfo(att.Listener, 1)); } } }
public List <Attention> QueryAll() { using (var dbcontext = new AttentionContext()) { return(dbcontext.Attentions.ToList()); } }
/// <summary> /// 初始化关注点数据库信息 /// </summary> public static void Init() { AttentionContext.CurrentDirectory = System.Environment.CurrentDirectory + @"\data\app\cc.wnapp.whuHelper"; using (var context = new AttentionContext()) { var objectContext = ((IObjectContextAdapter)context).ObjectContext; var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappingCollection.GenerateViews(new List <EdmSchemaError>()); } }
//添加关注 public void Add(String SourceQQ, String Attention, String GroupNum) { if (Attention.Contains(" ") || Attention.Equals("")) { throw new Exception("不允许输入空格和空字符串"); } using (var dbcontext = new AttentionContext()) { Attention newatt = new Attention(SourceQQ, GroupNum, Attention); dbcontext.Attentions.Add(newatt); dbcontext.SaveChanges(); this.Attentions = QueryAll(); } UpdateListeners(); }
//删除关注 public Boolean Remove(String SourceQQ, String Attention, String GroupNum) { using (var dbcontext = new AttentionContext()) { Attention temp = dbcontext.Attentions.FirstOrDefault(p => p.Listener == SourceQQ && p.Group == GroupNum && p.AttentionPoint == Attention); if (temp != null) { dbcontext.Attentions.Remove(temp); dbcontext.SaveChanges(); this.Attentions = QueryAll(); UpdateListeners(); return(true); } else { return(false); } } }
//更新关注 public Boolean Update(String SourceQQ, String OldAttention, String NewAttention, String GroupNum) { //检查是否存在, //如有删除之,并插入新的 //允许GroupNum为空,这样就只要查询所有的Attention字段相等的部分 using (var dbcontext = new AttentionContext()) { var quary = dbcontext.Attentions .Where(att => att.AttentionPoint == OldAttention && att.Group == GroupNum); foreach (Attention att in quary) { dbcontext.Attentions.Remove(att); dbcontext.Attentions.Add(new Attention(SourceQQ, GroupNum, NewAttention)); } dbcontext.SaveChanges(); this.Attentions = QueryAll(); } return(true); }