I work on a MVC 5 project with EF6.1. As a beginner, I try to keep the code generated by the MVC 5 template in order to use the default authentification system with AccountController. However, I had to add some classes, like "User.cs" as below, so I'm using Entity Framework Data Model.
public partial class User
{
public User()
{
this.Logs= new HashSet<Log>();
}
public int IdUser { get; set; }
public string AspNetUsersId { get; set; } //References to AspNetUsers.Id
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Job{ get; set; }
public virtual ICollection<Log> Logs { get; set; }
}
So, I have two contexts :
public partial class MyEFDataModelContainer : DbContext
{
public MyEFDataModelContainer ()
: base("name=MyEFDataModelContainer")
{
}
public DbSet<User> Users { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
So, my scenario is that I need to create an AspNetUsers, then add a AspNetRoles to this AspNetUsers and then create a User. We suppose the AspNetUsers creation and AspNetRoles works, but if the User creation fails, how can I rollback the AspNetUsers and AspNetRoles?
Can I create a transaction with these two contexts?
Unfortunately, I also need to call WCF webservices to execute this task? What can I do?
Best regards,
Updated
My merged contexts:
public partial class User: IdentityUser { }
public partial class MyEFDataModelContainer : IdentityDbContext<User>
{
public MyEFDataModelContainer() : base("name=MyEFDataModelContainer")
{ }
}
My AccountController.cs:
public class AccountController : Controller
{
public AccountController()
{
UserManager =new UserManager<User>(new UserStore<User>(new MyEFDataModelContainer()))
}
public UserManager<User> UserManager {get;set;}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new USER() { UserName = model.UserName, Email = model.Email, PhoneNumber = model.Phone };
var result = await UserManager.CreateAsync(user, model.Password);
var role = await UserManager.AddToRoleAsync(user.Id, "Membre");
if (result.Succeeded)
{
using (UserBusiness business = new UserBusiness())
{
var userResult = business.AddUser(model.FirstName, model.LastName, user.Id);
}
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
return View(model);
}
}
My Web.config :
<connectionStrings>
<add name="MyEFDataModelContainer" connectionString="metadata=res://*/MyEFDataModel.csdl|res://*/MyEFDataModel.ssdl|res://*/MyEFDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
But now, I have the error The specified type member ' UserName ' is not supported in LINQ to Entities . Only initializers , entity members , and entity navigation properties are supported . at the following line:
var result = await UserManager.CreateAsync(user, model.Password);