Light12306/Web12306/Models/ChatRoomDb.cs
2015-03-13 19:25:08 +08:00

80 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Web;
namespace Web12306.Models
{
using System.Data.Entity;
using System.Linq;
using Web12306.Models.Entity;
public class ChatRoomDb : DbContext
{
public ChatRoomDb()
: base("chatroom")
{
//I want this:
//AbuseReports.FirstOrDefault();
}
/// <summary>
/// This method is called when the model for a derived context has been initialized, but
/// before the model has been locked down and used to initialize the context. The default
/// implementation of this method does nothing, but it can be overridden in a derived class
/// such that the model can be further configured before it is locked down.
/// </summary>
/// <remarks>
/// Typically, this method is called only once when the first instance of a derived context
/// is created. The model for that context is then cached and is for all further instances of
/// the context in the app domain. This caching can be disabled by setting the ModelCaching
/// property on the given ModelBuidler, but note that this can seriously degrade performance.
/// More control over caching is provided through use of the DbModelBuilder and DbContextFactory
/// classes directly.
/// </remarks>
/// <param name="modelBuilder">The builder that defines the model for the context being created. </param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var cfg = modelBuilder.Configurations;
cfg.Add(new AbuseReportConfiguration());
cfg.Add(new ForbiddenUserConfiguration());
}
public DbSet<AbuseReport> AbuseReports { get; set; }
public DbSet<ForbiddenUser> ForbiddenUsers { get; set; }
public IList<AbuseReport> QueryAbuseReports(int type, int page, int? pagesize = null)
{
var offset = page > 1 ? (page - 1) * pagesize : 0;
var sql = "SELECT * FROM message_abusereport WHERE 1=1 ";
if (type != 0)
{
sql += " AND is_check=" + type;
}
sql += " ORDER BY ID DESC ";
if (pagesize.HasValue)
{
sql += "LIMIT " + offset + "," + pagesize;
}
sql += ";";
return Database.SqlQuery<AbuseReport>(sql).ToList();
}
public void MarkAllCheckedByReporter(string reporter)
{
Database.ExecuteSqlCommand("UPDATE `message_abusereport` set `is_check`=1 WHERE `reporting_account`='" + reporter + "'");
}
public void MarkAllCheckedByTarget(string reporter)
{
Database.ExecuteSqlCommand("UPDATE `message_abusereport` set `is_check`=1 WHERE `alleged_wrongdoer_account`='" + reporter + "'");
}
}
}