Error: Only primitive types or enumeration types are supported in this context EF

后端 未结 2 1006
-上瘾入骨i
-上瘾入骨i 2020-11-30 15:53

This piece of code keep making this error. :

Unable to create a constant value of type \'Repository.DBModel.Subscriber\'. Only primitive types or enum

相关标签:
2条回答
  • 2020-11-30 16:26

    You use a local collection, Subscribers, directly in a LINQ statement. But these objects can't be translated into SQL. There are only mappings from primitive types to database types.

    I'd suggest you use

    var emails = Subscribers.Select(s => s.Email).ToList();
    

    And proceed by using these strings (i.e. primitive values) in Contains statements like:

    var newSubscribers = db.Subscriber
                           .Where(dbSub => !emails.Contains(dbSub.Email))
                           .ToList();
    var updateSubscribers = db.Subscriber
                              .Where(dbSub => emails.Contains(dbSub.Email))
                              .ToList();
    
    0 讨论(0)
  • 2020-11-30 16:29

    Changing updateSubscribers to an IEnumerable will prevent this error

    0 讨论(0)
提交回复
热议问题