首页 新闻 赞助 找找看

asp.net mvc session 持久化到sql server

0
悬赏园豆:100 [已解决问题] 解决于 2016-05-04 17:24
  public ActionResult  AddToCart(Cart cart, Int32 productId, String returnUrl)
        {
            Product product = repository.Products.Where(p => p.ProductID == productId)
                .FirstOrDefault();
            if(product!=null)
            {
                cart.AddItem(product,1);
            }
            SetUserNameByViewBag();
          
            return PartialView(cart);
         }

 

自己的写了个购物篮的session,以及购物篮类的实现 cart类,之前一直是使用默认的session配置(sessionState mode="InProc"),现在打算持久化到sql server 数据库,修改了对应配置之后(

<sessionState timeout="60" mode="SQLServer" allowCustomSqlDatabase="true"
sqlConnectionString="Data Source=.;Initial Catalog=ASPState;Integrated Security=True"></sessionState>)session也没能持久化到数据库,但是点击添加到购物篮的按钮时候就不行,购物篮图标没有提示,退出程序重新打开 ,冲数据库加载session为空

 1 namespace BookStore.Domain.Entities
 2 {
 3     [Serializable]
 4    public  class Cart
 5     {
 6        private List<CartLine> lineCollection = new List<CartLine>();
 7        public void AddItem(Product product,Int32 quantity)
 8        {
 9            CartLine line = lineCollection.Where(p => p.Product.ProductID == product.ProductID)
10                .FirstOrDefault();
11            if(line==null)
12            {
13                lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
14            }
15            else
16            {
17                line.Quantity += quantity;
18            }
19        }
20        public void RemoveLine(Product product)
21        {
22            lineCollection.RemoveAll(p => product.ProductID == p.Product.ProductID);
23        }
24        public Decimal ComputerTotalValue()
25        {
26            return lineCollection.Sum(p => p.Product.Price * p.Quantity);
27        }
28        public void Clear()
29        {
30            lineCollection.Clear();
31        }
32        public IEnumerable<CartLine> Lines
33        {
34            get
35            {
36                return lineCollection;
37            }
38        }
39     }
40     [Serializable]
41    public class CartLine
42    {
43        public Product Product{get;set;}
44        public Int32 Quantity;
45    }
46 }
47 //session 的代码
48 namespace BookStore.WebUI.Binders
49 {
50     public class CartModelBinder:IModelBinder
51     {
52         private const String sessionKey = "Cart";
53         public object BindModel(ControllerContext controllerContext,
54             ModelBindingContext bingdingContext)
55         {
56             Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
57             if(cart==null)
58             {
59                 cart = new Cart();
60                 controllerContext.HttpContext.Session[sessionKey] = cart;
61             }
62             return cart;
63         }
64     }
65 }
blue.tian的主页 blue.tian | 初学一级 | 园豆:87
提问于:2016-04-29 19:20
< >
分享
最佳答案
0

是的  你需要预先在数据库添加相关表 才能使用数据库保存 Session 可点击下面的链接查看具体操作方法

http://www.cnblogs.com/rirusuo/archive/2013/11/19/3431446.html

收获园豆:60
z5337 | 小虾三级 |园豆:512 | 2016-04-29 20:58

我添加了 是默认的ASPState数据库表,,ASPStateTempSessions表的记录每当我用一个新的用户区登录会增加,,但增加后的记录无论我怎么修改session也不会变化,除了一些记录时间的属性变化。sessionItemShort的属性列没有变化,,

blue.tian | 园豆:87 (初学一级) | 2016-04-29 21:24
其他回答(2)
0

直接使用mongodb嘛

收获园豆:20
刘宏玺 | 园豆:14020 (专家六级) | 2016-04-29 20:53

现在还没考虑使用mongodb,,,现在先使用sqlserver

支持(0) 反对(0) blue.tian | 园豆:87 (初学一级) | 2016-04-29 20:55
0

非登录用户, 直接用session/cookie存

登录用户, 直接存sqlserver

收获园豆:20
walen | 园豆:429 (菜鸟二级) | 2016-05-01 21:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册