I've implemented class to create database for installation ...
It work great and it create database correctly.
The problem is when i try to insert default Data on empty database ...
1°) It add new permission correctly and return permission with ID = 1
2°) It add new role but it recreate new permission with ID = 2. (the permission values was replaced by new one)
3°) It add new account but it recreate new role with ID = 2 and new permission with ID = 3. (the permission and role values was replaced by new one)
then finally i have permission with ID = 3, role with ID = 2 and Account with ID = 1
Do you know how can i solve this?
I use Unity repository in my project
Thank you
It work great and it create database correctly.
The problem is when i try to insert default Data on empty database ...
1°) It add new permission correctly and return permission with ID = 1
2°) It add new role but it recreate new permission with ID = 2. (the permission values was replaced by new one)
3°) It add new account but it recreate new role with ID = 2 and new permission with ID = 3. (the permission and role values was replaced by new one)
then finally i have permission with ID = 3, role with ID = 2 and Account with ID = 1
Do you know how can i solve this?
I use Unity repository in my project
Thank you
public virtual void InstallData(string defaultUserName, string defaultUserPassword)
{
Permission permission = _permissionService.AddNewPermission("CanAccessAdminArea", "Can access admin area");
Role role = _roleService.AddNewRole("Supervisor", "Supervisor role", permission);
Account account = _accountService.AddNewAccount(defaultUserName, defaultUserPassword, role);
}
public Permission AddNewPermission(string Name, string Description)
{
Permission result = new Permission
{
Name = Name,
Description = Description
};
_permissionRepository.Insert(result);
return result;
}
public Role AddNewRole(string Name, string Description, Permission permission)
{
Role result = new Role
{
Name = Name,
Description = Description,
};
result.Permissions.Add(permission);
_roleRepository.Insert(result);
return result;
}
public Account AddNewAccount(string Username, string Password, Role role)
{
Account result = new Account
{
Username = Username,
Password = Password,
CreatedOn = DateTime.Now,
Role = role
};
_accountRepository.Insert(result);
return result;
}
public virtual void Insert(T entity)
{
try
{
if (entity == null)
throw new ArgumentNullException("entity");
this.Entities.Add(entity);
this._context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
var msg = string.Empty;
foreach (var validationErrors in dbEx.EntityValidationErrors)
foreach (var validationError in validationErrors.ValidationErrors)
msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
var fail = new Exception(msg, dbEx);
//Debug.WriteLine(fail.Message, fail);
throw fail;
}
}