简体   繁体   English

为什么实体框架不能识别数据库中的现有记录?

[英]Why doesn't Entity Framework recognise an existing record in the database?

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.我为图书馆创建了一个数据库系统,当用户注册时,程序会检查他们是否已经有一个帐户,但即使我可以在我的 DBMS 中看到记录存在,这总是返回 false。

 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另外,这是我的 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");
    }
}

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.首次连接到现有数据库时,此类问题通常是由于 EF 指向您不期望的数据库。 (Ie creating one via Code First rather than connecting to your existing DB, or creating tables you don't expect.) (即通过 Code First 创建一个,而不是连接到您现有的数据库,或创建您不期望的表。)

When using database first, you should always disable the Code First initialization:当首先使用数据库时,您应该始终禁用 Code First 初始化:

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.如果 DbContext 尝试创建一个有助于指导您进行更正的模式,这将生成一个异常。

By default EF will be looking for a connection string called "LibraryDbContext" in your web.confing/.exe.config file in the runtime location.默认情况下,EF 将在运行时位置的 web.confing/.exe.config 文件中查找名为“LibraryDbContext”的连接字符串。 When using connection strings I generally like to be explicit with the connection string name to reflect a Database rather than the DbContext.使用连接字符串时,我通常喜欢明确地使用连接字符串名称来反映数据库而不是 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.因此,例如,我将主数据库连接字符串称为“AppConnection”,并将其传递给基本构造函数。

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.或者,要测试您的连接,您可以将连接字符串本身传递给基本构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM