我需要一些我正在进行的MVC项目的帮助。我正在尝试创建一个卖家或买家类型的用户。我创建了多对多的关系,因为用户既可以是卖家也可以是买家。我的问题是插入我继续获得以下错误的角色类型
mscorlib.dll中出现未处理的“System.StackOverflowException”类型异常附加信息:集合属于 固定大小。
以下是我正在使用的代码
UserController中
[HttpPost]
public ActionResult CreateUser(UserModel data, string SellerType)
{
try
{
if(SellerType == "Buyer")
{
userType = 2;
}
else if (SellerType=="Seller")
{
userType = 3;
}
data.myUser.DateCreated = DateTime.Now;
new UsersService.UserBLClient().RegisterUser(data.myUser, userType);
}
catch
{
return View();
}
return RedirectToAction("Index", "Home");
}
服务引用
public RegistrationOutcome RegisterUser(Person newPerson,int userTypeId)
{
if (DoesUserNameExist(newPerson.Username) == true)
{
return RegistrationOutcome.UsernameExists;
throw new Exception("Username has already been registered");
}
if (DoesEmailExist(newPerson.Email) == true)
{
return RegistrationOutcome.EmailExists;
throw new Exception("Email has already been registered");
}
else
{
DaRole rr = new DaRole();
DaUser ur = new DaUser();
rr.DataContext = ur.DataContext;
ur.RegisterUser(newPerson);
rr.AllocateUser(newPerson, rr.GetRoleById(userTypeId));
return RegistrationOutcome.Success;
}
}
插入和获取用户角色
public void AllocateUser(Person newPerson, UserType roleType )
{
newPerson.UserType.Add(roleType);
DataContext.SaveChanges();
}
public UserType GetRoleById(int id)
{
return DataContext.UserType.SingleOrDefault(x => x.RoleID == id);
}