我想记录浏览我的网页的客户端,所以我在Global.asax的Session_Start()中这样做
String ipAddress = System.Web. HttpContext.Current.Request.
ServerVariables["REMOTE_ADDR"];
Web.Models.WebsiteTracking web_track = new Web.Models.WebsiteTracking();
web_track.IDUser = 0;
web_track.ActiveLoginDate = DateTime.Now;
web_track.IPAddress = ipAddress;
this.DataContext.WebsiteTrackings.AddObject(web_track);
this.DataContext.SaveChanges();
我将用户的IP地址,他们浏览的日期和ID = 0记录到表WebsiteTracking。但它错误:不包含定义DataContext而没有扩展方法'DataContext'。
任何人都可以告诉我,我必须用“this.DataContext”代替哪一件事。
非常感谢你。
答案 0 :(得分:1)
尝试创建DataContext
。
String ipAddress = System.Web. HttpContext.Current.Request.
ServerVariables["REMOTE_ADDR"];
Web.Models.WebsiteTracking web_track = new Web.Models.WebsiteTracking();
web_track.IDUser = 0;
web_track.ActiveLoginDate = DateTime.Now;
web_track.IPAddress = ipAddress;
using(var dataContext = new DataContext())
{
dataContext.WebsiteTrackings.AddObject(web_track);
dataContext.SaveChanges();
}