本文整理汇总了C#中UsersContext.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# UsersContext.SaveChanges方法的具体用法?C# UsersContext.SaveChanges怎么用?C# UsersContext.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UsersContext
的用法示例。
在下文中一共展示了UsersContext.SaveChanges方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteProfiles
public override int DeleteProfiles(string[] usernames)
{
using (UsersContext db = new UsersContext())
{
List<UserProfile> profiles = new List<UserProfile>();
int i;
for (i = 0; i < usernames.Count(); i++)
{
string userName=usernames[i];
profiles.Add(db.UsersProfiles.Find(userName));
}
db.UsersProfiles.RemoveRange(profiles);
db.SaveChanges();
return i;
}
}
示例2: AddUser
public bool AddUser(RegisterExternalLoginModel model)
{
using (var db = new UsersContext())
{
var user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Check if user already exists
if (user == null)
{
// Insert name into the profile table
db.UserProfiles.Add(new UserProfile {UserName = model.UserName});
db.SaveChanges();
return true;
}
return false;
}
}
示例3: RemoveInactiveUsers
private static void RemoveInactiveUsers(UsersContext context, DateTime logDate)
{
List<User> users = context.Users.Where(user => user.LastTimeLoggedIn < logDate && !user.IsDeleted).ToList();
foreach (User user in users)
{
user.IsDeleted = true;
}
if (users.Count == 0)
{
Console.WriteLine("No users have been deleted");
}
else
{
Console.WriteLine($"{users.Count} user has been deleted");
}
context.SaveChanges();
}
示例4: AddUsersToRoles
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
using (UsersContext db = new UsersContext())
{
foreach (string username in usernames)
{
foreach (string rolename in roleNames)
{
UserInRole userInRole = new UserInRole();
userInRole.UserName = (string) Membership.GetUser(username).ProviderUserKey;
userInRole.RoleId = db.Roles.FirstOrDefault(x => x.Name == rolename).Id;
db.UsersInRoles.Add(userInRole);
db.SaveChanges();
}
}
}
}
示例5: AuthenticateSession
public void AuthenticateSession(string userId, string ipAddress, string sessionId)
{
using (var ctx = new UsersContext())
{
DateTime timeout = DateTime.UtcNow.AddSeconds(-TimeoutSeconds);
var usrSession = ctx.UserSessions.FirstOrDefault(s => s.SqrlId == sessionId &&
s.AuthenticatedDatetime == null &&
s.CreatedDatetime >= timeout);
if (usrSession == null)
{
return;
}
usrSession.AuthenticatedDatetime = DateTime.UtcNow;
usrSession.UserId = userId;
ctx.SaveChanges();
}
var hubContext = GlobalHost.ConnectionManager.GetHubContext<LoginHub>();
hubContext.Clients.Group(sessionId).login();
}
示例6: StartSession
public void StartSession(string sessionId)
{
string httpSessionId = HttpContext.Current.Session.SessionID;
using (var ctx = new UsersContext())
{
var session = ctx.UserSessions.Find(httpSessionId);
if (session == null)
{
session = new UserSession
{
SessionId = httpSessionId
};
ctx.UserSessions.Add(session);
}
session.AuthenticatedDatetime = null;
session.SqrlId = sessionId;
session.CreatedDatetime = DateTime.UtcNow;
ctx.SaveChanges();
}
}
示例7: ExternalLoginConfirmation
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
{
string provider = null;
string providerUserId = null;
if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
{
return RedirectToAction("Manage");
}
if (ModelState.IsValid)
{
// Insert a new user into the database
using (UsersContext db = new UsersContext())
{
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Check if user already exists
if (user == null)
{
// Insert name into the profile table
db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
db.SaveChanges();
OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
}
}
}
ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
示例8: ExternalLoginConfirmation
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
{
string provider = null;
string providerUserId = null;
if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
{
return RedirectToAction("Manage");
}
if (ModelState.IsValid)
{
// Добавление нового пользователя в базу данных
using (UsersContext db = new UsersContext())
{
UserProfile user = db.UsersProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Проверка наличия пользователя в базе данных
if (user == null)
{
// Добавление имени в таблицу профиля
db.UsersProfiles.Add(new UserProfile { UserName = model.UserName });
db.SaveChanges();
OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("UserName", "Имя пользователя уже существует. Введите другое имя пользователя.");
}
}
}
ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
示例9: GetAuthenticatedSession
private UserSession GetAuthenticatedSession()
{
string sessionId = Session.SessionID;
using (var ctx = new UsersContext())
{
UserSession session =
ctx.UserSessions.FirstOrDefault(s => s.SessionId == sessionId && s.AuthenticatedDatetime != null);
if (session != null)
{
ctx.UserSessions.Remove(session);
ctx.SaveChanges();
}
return session;
}
}
示例10: Register
public ActionResult Register(RegisterModel model)
{
using (var db = new ProjectsContext())
{
if (!ModelState.IsValidField("UserDepartment"))
{
// 设置部门
var departmentId = int.Parse(Request.Params["UserDepartment"]);
var userDepartment = db.UserDepartments.First(d => d.DepartmentId == departmentId);
if (userDepartment == null)
{
ModelState.AddModelError("UserDepartment", "部门模型转换失败");
}
else
{
model.UserDepartment = userDepartment;
ModelState.Remove("UserDepartment");
}
}
}
using (var db = new UsersContext())
{
if (ModelState.IsValid)
{
// 尝试注册用户
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { RealName = model.RealName }, true);
var userProfile = db.UserProfiles.First(d => d.UserName == model.UserName);
userProfile.UserDepartment = model.UserDepartment;
db.SaveChanges();
//WebSecurity.Login(model.UserName, model.Password);
//return RedirectToAction("Index", "Home");
ModelState.AddModelError("", "账号已创建,请等待系统管理员激活。");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
}
示例11: SetPropertyValues
public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
{
using (UsersContext db = new UsersContext())
{
string userName = context["UserName"].ToString();
UserProfile userProfile = db.UsersProfiles.Find(userName);
if (userProfile == null)
{
userProfile = new UserProfile();
foreach (SettingsPropertyValue profilePropertyValue in collection)
{
userProfile.GetType().GetProperty(profilePropertyValue.Name).SetValue(userProfile, profilePropertyValue.PropertyValue, null);
}
db.UsersProfiles.Add(userProfile);
}
else
{
foreach (SettingsPropertyValue profilePropertyValue in collection)
{
userProfile.GetType().GetProperty(profilePropertyValue.Name).SetValue(userProfile, profilePropertyValue.PropertyValue, null);
}
}
db.SaveChanges();
}
}
示例12: ExternalLoginConfirmation
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
{
string provider = null;
string providerUserId = null;
if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
{
return RedirectToAction("Manage");
}
if (ModelState.IsValid)
{
// Insérer un nouvel utilisateur dans la base de données
using (UsersContext db = new UsersContext())
{
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Vérifier si l'utilisateur n'existe pas déjà
if (user == null)
{
// Insérer le nom dans la table des profils
db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
db.SaveChanges();
OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("UserName", "Le nom d'utilisateur existe déjà. Entrez un nom d'utilisateur différent.");
}
}
}
ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
示例13: RemoveUsersFromRoles
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
using (UsersContext db = new UsersContext())
{
List<UserInRole> usersInRoles = new List<UserInRole>();
for (int i = 0; i < usernames.Count(); i++)
{
string userName = usernames[i];
for (int j = 0; j < roleNames.Count(); j++)
{
string roleName = roleNames[j];
int roleId = (int) db.Roles.FirstOrDefault(x=>x.Name==roleName).Id;
UserInRole userInRole = db.UsersInRoles.FirstOrDefault(x => x.UserName == userName && x.RoleId == roleId);
usersInRoles.Add(userInRole);
}
}
db.UsersInRoles.RemoveRange(usersInRoles);
db.SaveChanges();
}
}