如何使用Identity表和Custom表更新Asp.Net MVC 5中的Many to Many表

时间:2017-02-16 16:28:13

标签: asp.net-mvc asp.net-mvc-5

我需要与Identity表和Custom表建立多对多的关系 我在answer中使用了相同的逻辑 但问题是我无法在新的多对多表中添加任何内容

这是我自己的代码

ApplicationUser

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        subjects = new HashSet<subject>();
    }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public virtual ICollection<subject> subjects { get; set; }
}

主题

 public class subject
    {        
        public int subjectId { get; set; }
        public string subjectCode { get; set; }
        public virtual ICollection<ApplicationUser> buyers { get; set; }
    }

我厌倦了添加实体的功能 假设接收用户和subjectCode的电子邮件,并在表格中将用户和主题链接在一起

public ActionResult addBuyer(FormCollection fc)
{
    ApplicationUser tst = _context.Users.FirstOrDefault(b => b.Email.Equals(fc["Email"].ToString()));
    var temp = _context.subjects.Include("buyers").Where(c => c.subjectCode.Equals(fc["SubCode"].ToString()) );
    temp.First().buyers.Add(tst);
    return RedirectToAction("Index");
}

,这是错误

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method, and this method cannot be translated into a store expression.

0 个答案:

没有答案