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(); } /// /// 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. /// /// /// 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. /// /// The builder that defines the model for the context being created. protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var cfg = modelBuilder.Configurations; cfg.Add(new AbuseReportConfiguration()); cfg.Add(new ForbiddenUserConfiguration()); } public DbSet AbuseReports { get; set; } public DbSet ForbiddenUsers { get; set; } public IList 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(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 + "'"); } } }