0

I have created a database system for a library, and when a user registers the program checks if they already have an account, but this is always returning false even when I can see in my DBMS that the record exists.

 public static bool UserExists(string email)
 {
            using (var context = new LibraryDbContext())
            {
                if (context.Users.Any(d => d.Email == email))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
 }

Also, this is my Dbcontext class

public class LibraryDbContext : DbContext
{
    public LibraryDbContext()
        : base()
    {
    }

    public DbSet<Book> Books { get; set; }

    public DbSet<User> Users { get; set; }

    public DbSet<ReturnsLog> Returns { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseInMemoryDatabase("LibraryProjectDbo");
    }
}
2
  • If that is all you have in your dbcontext file then you need to see here Commented Jun 24, 2020 at 15:53
  • thanks, I've added the OnModelCreating function from that page, but that doesn't seem to have changed anything. Is there anything in particular I need to add? Commented Jun 24, 2020 at 16:10

1 Answer 1

2

Typically issues like this when connecting to an existing database for the first time are due to EF being pointed at a database you don't expect. (I.e. creating one via Code First rather than connecting to your existing DB, or creating tables you don't expect.)

When using database first, you should always disable the Code First initialization:

public LibraryDbContext()
{
    Database.SetInitializer<LibraryDbContext>(null);
}

This will generate an exception if the DbContext goes and tries to create a schema which will help direct you to the correction.

By default EF will be looking for a connection string called "LibraryDbContext" in your web.confing/.exe.config file in the runtime location. When using connection strings I generally like to be explicit with the connection string name to reflect a Database rather than the DbContext. Often I will utilize multiple contexts to split up behaviour in a system that all point to the same database. So for example I'd call the main database connection string something like "AppConnection" and pass that through the base constructor.

public LibraryDbContext()
    : base ("AppConnection")
{
    Database.SetInitializer<LibraryDbContext>(null);
}

Alternatively to test your connection out you can pass a Connection String itself to the base constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! for some reason my base() won't accept a string parameter, which libraries are you using for EF? (I'm using Microsoft.EntityFrameworkCore)
Ah, sorry.. that applied to EF6 which I still use for most of my projects.. For EF Core you use the OnConfiguring override: (learnentityframeworkcore.com/connection-strings) From there you can point it at a connection string setting or hard-coded connection string to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.