0

I have a c# DbContext defined like this:

public class DataContext : DbContext
{
        public DbSet<Account> Accounts { get; set; }        
        ...     
}

In another place I have instance of DataContext which I use to retrieve all my Account objects - and I expect many of them, but the code below:

var accountAll = context.Accounts.ToArray();

throws an exception:

System.TypeInitializationException: 'The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception.'

Inner Exception
InvalidOperationException: Sequence contains more than one matching element

Can someone understand why exception is thrown?

Class Account:

using System;
using System.Collections.Generic;

namespace WebApi.Entities
{
    public class Account
    { 
        public int Id { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string PasswordHash { get; set; }
        public bool AcceptTerms { get; set; }
        public Role Role { get; set; }
        public string VerificationToken { get; set; }
        public DateTime? Verified { get; set; }
        public bool IsVerified => Verified.HasValue || PasswordReset.HasValue;
        public string ResetToken { get; set; }
        public DateTime? ResetTokenExpires { get; set; }
        public DateTime? PasswordReset { get; set; }
        public DateTime DOB { get; set; }
        public DateTime Created { get; set; }
        public DateTime? Updated { get; set; }

        // Notification section
        public bool NotifyWeekBefore { get; set; } = true;
        public bool NotifyThreeDaysBefore { get; set; } = true;
        // End of Notification section

        public List<Schedule> Schedules { get; set; }
        public List<Function> UserFunctions { get; set; }
        public List<RefreshToken> RefreshTokens { get; set; }

        public bool OwnsToken(string token) 
        {
            return this.RefreshTokens?.Find(x => x.Token == token) != null;
        }
    }
}
Janusz Dalecki
  • 197
  • 1
  • 10
  • Exactly what version of EF are you using? Can you post a minimal, complete repro? – David Browne - Microsoft Jun 29 '23 at 14:03
  • Does `Account` have any type initialization logic, or is it just a simple POCO? – David Jun 29 '23 at 14:03
  • Please show class account with text – abolfazl sadeghi Jun 29 '23 at 14:41
  • [abolfazl-sadeghi](https://stackoverflow.com/users/10193401/abolfazl-sadeghi) OK. I added class `Account` at the end of original question. – Janusz Dalecki Jun 29 '23 at 19:08
  • @JanuszDalecki ,I added Account in my project and Tests ,your code correct,i think other part of project incorrect – abolfazl sadeghi Jun 29 '23 at 19:26
  • I used EF Core 7 ,Which version do you use? do you use EF or EF Core? – abolfazl sadeghi Jun 29 '23 at 19:31
  • [abolfazl-sadeghi](https://stackoverflow.com/users/10193401/abolfazl-sadeghi) I use target framework .NET 7.0 - is that what you are asking for?. How do I check EF version? – Janusz Dalecki Jun 29 '23 at 19:34
  • https://stackoverflow.com/questions/3377821/determine-version-of-entity-framework-i-am-using#:~:text=Another%20way%20to%20get%20the,version%20the%20project%20has%20installed. – abolfazl sadeghi Jun 29 '23 at 19:35
  • I use EF version 6 – Janusz Dalecki Jun 29 '23 at 19:39
  • [david-browne](https://stackoverflow.com/users/7297700/david-browne-microsoft) I am using EF 6.0. Account is just POCO. I would have to built some repro minimal project as this one is quite verbose. Do you want me to try to do so? One thing to note that I have been using .NET 3.1 and I migrated my code today to .NET 7.0. The code still works properly if I use it with .NET 3.1 – Janusz Dalecki Jun 29 '23 at 19:41
  • I think,If you can immigrate to EF Core 7,your code will be correct – abolfazl sadeghi Jun 29 '23 at 19:57
  • Do you have any explicit configuration for this either in the DbContext `OnModelCreating ` or an EntityTypeConfiguration for account? For instance the IsVerified property should be marked as ignored/not mapped. Is The Id column in the account table the actual PK or is the table using something like a composite PK (multiple columns) that you need to configure EF to use? – Steve Py Jun 30 '23 at 00:02
  • 1
    Problem solved. Once I migrated to EF 7.0 there are no exceptions anymore. Thanks everybody who replied to my question. – Janusz Dalecki Jun 30 '23 at 00:36

0 Answers0