当我尝试在程序包管理器中执行命令add-migration时,出现以下错误。
表达式'a => a.Customer'不是有效的属性表达式。该表达式应表示一个简单的属性访问:“ t => t.MyProperty”。
以及我在其中定义表之间关系的BankContext.cs类的代码
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();
}
}
最后但并非最不重要的是,我的Account类中发生所有麻烦的代码
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;
}
}
我已经尝试过将客户资产设置为“虚拟”,但无法使用。
答案 0 :(得分:0)
您需要将public Customer Customer;
更改为public Customer Customer { get; set; }
。当前Customer
是 field 而不是属性。