【问题标题】:how to update Many to Many table in Asp.Net MVC 5 with Identity table and Custom table如何使用标识表和自定义表更新 Asp.Net MVC 5 中的多对多表
【发布时间】:2017-02-16 16:28:12
【问题描述】:

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

这是我自己的代码

应用程序用户

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; }
    }

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

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.

【问题讨论】:

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


    【解决方案1】:

    由于fc["Email"] 和fc["SubCode"] 集合索引器在 LINQ to Entities 上可能没有等效的 SQL 语法,因此请尝试将普通值从索引器分配/提取到字符串变量中并将它们传递给查询:

    public ActionResult addBuyer(FormCollection fc)
    {
        // assign ToString() methods into string variables
        String email = fc["Email"].ToString();
        String subCode = fc["SubCode"].ToString();
    
        // pass only plain values into the query
        ApplicationUser tst = _context.Users.FirstOrDefault(b => b.Email.Equals(email));
        var temp = _context.subjects.Include("buyers").Where(c => c.subjectCode.Equals(subCode));
        temp.First().buyers.Add(tst);
        return RedirectToAction("Index");
    }
    

    注意:您应该避免在 LINQ 查询中调用this post 中提到的ToString() 方法,使用SqlFunctions.StringConvert 或其他一些聚合方法作为替代方法。

    相关问题:

    LINQ to Entities does not recognize the method 'System.String get_Item (System.String)',

    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

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2014-04-24
      • 1970-01-01
      • 2018-09-03
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多