/// <summary> /// The basic function used to create new entities. /// </summary> /// <param name="entitiy">The entity we will add to the database.</param> /// <returns>The create entitiy.</returns> public Category Create(Category entitiy) { // add the category to the context var storedCategory = _context.Categories.Add(entitiy); // save the changes and return the newly created entitiy _context.SaveChanges(); return(storedCategory); }
/// <summary> /// The basic function used to create new entities. /// </summary> /// <param name="entitiy">The entity we will add to the database.</param> /// <returns>The create entitiy.</returns> public Keyword Create(Keyword entitiy) { // add the keyword to the context var storedKeyword = _context.Keywords.Add(entitiy); // save the changes and return the newly created entitiy _context.SaveChanges(); return(storedKeyword); }
/// <summary> /// The basic function used to create new entities. /// </summary> /// <param name="entitiy">The entity we will add to the database.</param> /// <returns>The create entitiy.</returns> public Document Create(Document entitiy) { // add the document to the context var storedDocument = _context.Documents.Add(entitiy); // save the changes and return the newly created entitiy _context.SaveChanges(); // reset the context so we flush the object state manager // TODO: Bug because we might create new keywords/categories as we create documents // TODO : Look into different flow for document creation //_context = new DsContext(); return(storedDocument); }
/// <summary> /// Add a single role to the system uniquely defined by the role alias, with the given role properties /// </summary> /// <param name="context"></param> /// <param name="roleTitle"></param> /// <param name="roleAlias"></param> /// <param name="roleDescription"></param> public static void AddRoleToContext(DsContext context, string roleTitle, string roleAlias, string roleDescription = "") { // Initially check if the role has been already added // only only add the role if its not been previously added // again based on the alias if (!context.Roles.Any(x => x.Alias == roleAlias)) { var role = new Role(); role.Alias = roleAlias; role.Title = roleTitle; role.Description = roleDescription; context.Roles.Add(role); context.SaveChanges(); } }
public string InsertCustomer(Customer cust) { try { var dbContext = new DsContext(); Customer custTbl = new Customer(); custTbl.CustomerName = cust.CustomerName; custTbl.CustomerAddress = cust.CustomerAddress; custTbl.EmailId = cust.EmailId; dbContext.Customer.Add(custTbl); dbContext.SaveChanges(); return("Customer Saved Successfully"); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// Add a User Entitiy with the given user properties /// </summary> /// <param name="context">The context to which we will be adding User Data </param> /// <param name="username">The username for the created User</param> /// <param name="password">The password for the created User</param> /// <param name="email">The email for the created User</param> /// <param name="featureTierAlias">The alias for the feature tier that will be linked with the user.</param> /// <param name="roleAliases">The collection of role aliases for the roles that will be linked with the user.</param> public static void AddUser(DsContext context, string username, string password, string email, string featureTierAlias = "", params string[] roleAliases) { // only add the user if there is no such user based on the username if (context.Users.FirstOrDefault(x => x.Username == username) == null) { var newUser = new User() { Username = username, Password = password, Email = email }; // Add the feature tier based on the alias if such tier exsists // Note the tier must be created before atempting to link it to the User var tier = context.UserFeatureTiers.FirstOrDefault(x => x.TierAlias == featureTierAlias); if (tier != null) { newUser.UserFeatureTier = tier; } // Link users to existing roles based on the aliases if the role exists newUser.UserRoles = new List <Role>(); foreach (var roleAlias in roleAliases) { var role = context.Roles.FirstOrDefault(x => x.Alias == roleAlias); if (role != null) { newUser.UserRoles.Add(role); } } // add the user to the context context.Users.AddOrUpdate(newUser); // Save the user changes after adding each user entitiy context.SaveChanges(); } }
/// <summary> /// Add a single Feature Tier with the given properties. /// </summary> /// <param name="context"></param> /// <param name="tierName"></param> /// <param name="tierAlias"></param> /// <param name="tierDescription"></param> /// <param name="downloadsPerDay"> </param> /// <param name="upvotesPerDay"> </param> /// <param name="downvotesPerDay"> </param> public static void AddFeatureTier(DsContext context, string tierName, string tierAlias, string tierDescription = "", int downloadsPerDay = 5, int upvotesPerDay = 5, int downvotesPerDay = 5) { // we will only add a tier if its not been added before // based on the tier alias if (!context.UserFeatureTiers.Any(x => x.TierAlias == tierAlias)) { var userFeatureTier = new UserFeatureTier(); userFeatureTier.TierAlias = tierAlias; userFeatureTier.TierDescription = tierDescription; userFeatureTier.TierName = tierName; userFeatureTier.DocumentDownloadsPerDay = downloadsPerDay; userFeatureTier.DocumentUpvotesPerDay = upvotesPerDay; userFeatureTier.DocumentDownvotesPerDay = downvotesPerDay; context.UserFeatureTiers.Add(userFeatureTier); context.SaveChanges(); } }
/// <summary> /// Save context changes from the given repository. /// </summary> public void SaveChanges() { _dsContext.SaveChanges(); }