问题描述
正如标题所说我使用新的 C#
MVC 5
身份
做一个简单的电话:
As the title says I am using the new C#
MVC 5
Identity
doing a simple call:
UserManager.AddToRole(User.Id, User.RequestedRole);
我的方法我视图模型
是从叫控制器
的的UserManager
在基类
我的视图模型$ C的创建$ C>是这样的:
UserManager = new UserManager<TMUser>(new UserStore<TMUser>(new TMContext()));
当我做出 AddToRole
方法上面的电话,我得到这个内部异常
(外一种是通用/没用):
When I make the above call to AddToRole
method, I get this Inner Exception
(The outer one is generic/useless):
{"A relationship from the 'Ledger_User' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Ledger_User_Source' must also in the 'Deleted' state."}
我显然不是删除所有什么,但只增加一个角色,我的用户。我有,当我试图从混合多个上下文对象之前,此异常......但我不这样做,在这里......请帮助。
I'm obviously not deleting anything at all but only adding a role to my user. I've had this exception before when I am trying to mix objects from multiple contexts...but I'm not doing that here...please help.
编辑:
我在它被干扰的情况下摆脱了模型,并添加以下code到我的控制器:
I've gotten rid of the model in case it was interfering and added the following code to my controller:
public ActionResult UpdateRoles(string id)
{
if (ModelState.IsValid)
{
var userManager = new UserManager<TMUser>(new UserStore<TMUser>(new TMContext()));
var userRequestingRole = userManager.FindById(id);
if (!userManager.IsInRole(userRequestingRole.Id, userRequestingRole.RequestedRole))
userManager.AddToRole(userRequestingRole.Id, userRequestingRole.RequestedRole);
// It is still crashing with the same error in the above AddToRole
}
有关详细信息,这里是我的 TMUser
和总帐
对象的结构:
For further information, here is the structure of my TMUser
and Ledger
objects:
public class TMUser : IdentityUser
{
public TMUser()
{
Ledger = new Ledger();
OrderHistory = new List<Order>();
Clients = new List<Client>();
IsActive = true;
}
[DisplayName("Full Name")]
public string FullName { get; set; }
[DisplayName("Notification Email")]
public string NotificationEmail { get; set; }
public virtual Ledger Ledger { get; set; }
public virtual List<Order> OrderHistory { get; set; }
public virtual List<Client> Clients { get; set; }
public string RequestedRole { get; set; }
public virtual TMUser RoleApprovedBy { get; set; }
public bool IsActive { get; set; }
}
public class Ledger
{
public Ledger()
{
Transactions = new List<Transaction>();
}
public long Id { get; set; }
[Required]
public virtual TMUser User { get; set; }
public virtual List<Transaction> Transactions { get; set; }
public decimal GetBalance()
{
// ...
}
internal void AddTransaction(decimal amount, string description, Order order)
{
// ...
}
}
另一个编辑:
今天又是令人沮丧的一天。在我的上下文做一些改变后
它最初好像我解决了这一问题。这里是我所做的更改:
Another Today was another frustrating day. After making some changes in my Context
it initially seemed like I fixed the problem. Here is the change I made:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TMUser>().HasOptional(c => c.RoleApprovedBy);
modelBuilder.Entity<TMUser>().HasOptional(c => c.Ledger);
}
我添加了上面的DB Context类,矿:公共类TMContext:IdentityDbContext&LT; TMUser&GT;
这工作的第一次,我必须打破某种关联呢?然而,当我用不同的用户,类似的又试了一次,但略有不同的例外
发生了:
This worked the first time, I must have broken some kind of an association? However, when I tried again with a different user, a similar, but slightly different Exception
happened:
{"A relationship from the 'TMUser_Ledger' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'TMUser_Ledger_Target' must also in the 'Deleted' state."}
所以感觉就像我回到了起点......我可以继续通过删除总帐
从用户$ C会$ C>对象,但是这将是欺骗......我真的不想让哈克与它...请帮助...
So it feels like I am back to square one...I can keep going by removing the Ledger
from the User
object, but that would be cheating...I really don't want to get hacky with it...please help...
推荐答案
的问题是,您在TMUser的构造函数创建一个新的总帐,当你这样做,你将删除当前的分类帐的TMUser并把它替换一个新的空单。然后,EF将处理新分类帐为需要在数据库中插入新的对象。这就是为什么你得到关于为en删除状态的实体验证错误。
The problem is that you create a new Ledger in the constructor of the TMUser, when you do that you will remove the current ledger for the TMUser and replace it with a new empty one. And then, EF will handle the new Ledger as new object that needs to be inserted in the database. Thats why you are getting the validation error about an entity that is en deleted state.
在TMUser的构造函数创建一个新的总帐的另一件事情引起每一个TMUser有总帐的效果,但在你的数据库模型,你已经将它设置为可以为空(bacause的HasOptional的)。
Another thing with creating an new Ledger in the constructor of TMUser causes the effect that every TMUser has a ledger but in your database model you have set it to nullable (bacause of the HasOptional).
这篇关于身份UserManager.AddToRole抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!