public void Add(User user) { if (user == null) { return; } if (string.IsNullOrWhiteSpace(user.UserName) || string.IsNullOrWhiteSpace(user.PassWord)) { throw new ArgumentNullException("名称或密码不能为空"); } var olduser = _db.Users.SingleOrDefault(n => n.UserName == user.UserName); if (olduser != null) { throw new ArgumentNullException("该用户名已经存在"); } ; user.PassWord = Encrypt.GetMd5Code(user.PassWord); _db.Users.Add(user); _db.SaveChanges(); }
public void DataBaseInit() { using (var db = new XNDb()) { if (db.Users.FirstOrDefault(n => n.UserName == "Admin") == null) { db.Users.Add(new User() { UserName = "******", Description = "管理员", PassWord = Encrypt.GetMd5Code("admin"), }); db.SaveChanges(); } } }
public void Add(Product product) { if (product == null) { return; } var olduser = _db.Products.SingleOrDefault(n => n.ProductName == product.ProductName); if (olduser != null) { throw new ArgumentNullException("该产品已经存在"); } _db.Products.Add(product); _db.SaveChanges(); }