EF Core 模型构建器不是有效的流利表达 api

EF Core modelbuilder not a valid expression fluent api

当我尝试在我的包管理器中执行命令 add-migration 时,出现以下错误。

The expression 'a => a.Customer' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'.

这是 class 图的图像

和我的 BankContext.cs class 的代码,我在其中定义表之间的关系

namespace Bank.Datalayer
{
    public class BankContext : DbContext
    {
        public DbSet<Account> Accounts { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<Customer> Customers { get; set; }
        // TODO: Vul deze klasse aan
        public BankContext() { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server = (localdb)\mssqllocaldb; Database =  BankDB; Trusted_Connection = true");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //Primary key
        modelBuilder.Entity<City>()
            .HasKey(c => c.ZipCode);
        modelBuilder.Entity<Account>()
            .HasKey(a => a.Id);
        modelBuilder.Entity<Customer>()
            .HasKey(c => c.CustomerId);

        //Foreign Keys
        modelBuilder.Entity<Account>()
            .HasOne(a => a.Customer)
            .WithMany(c => c.Accounts)
            .HasForeignKey(c => c.CustomerId);


        modelBuilder.Entity<City>()
            .HasMany(c => c.Customers)
            .WithOne(customer => customer.City)
            .HasForeignKey(c => c.Name);

        base.OnModelCreating(modelBuilder);
    }

    public void CreateOrUpdateDatabase()
    {
        Database.Migrate();
    }
}

最后但并非最不重要的是我的帐户代码 class 所有问题都发生在这里

namespace Bank.DomainClasses
{
    public class Account
    {
        // TODO: vul deze klasse aan
        public int Id { get; set; }
        public int AccountNumber { get; set; }
        public decimal Balance { get; set; }
        public AccountType AccountType { get; set; }
        public int CustomerId { get; set; }

        public Customer Customer;
    }
}

我已经试过让客户 属性 "virtual" 但是没用。

您需要将 public Customer Customer; 更改为 public Customer Customer { get; set; }。当前 Customer 字段 而不是 属性.