在我的EF Core 3.1的代码优先实现中,我具有以下三个实体:
[Table("Customers")]
public class Customer
{
public int Id { get; set; }
public virtual ICollection<CustomerAddress> Addresses { get; set; } = new HashSet<CustomerAddress>();
}
[Table("Address_Customers")]
public class CustomerAddress : AddressBase
{
[ForeignKey("FK_Address_Customers_CustomerId_Customers_Id")]
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
[Table("Addresses")]
public abstract class AddressBase
{
public int Id { get; set; }
public Nullable<bool> Active { get; set; }
[MaxLength(50)]
public string StreetAddress { get; set; }
}
这是我的代码:
var customer = new Customer() { Name = "GetAddresses", CreatedAt = new DateTimeOffset(DateTime.Now) };
context.Customers.Add(customer);
context.SaveChanges();
customer.Addresses.Add(new CustomerAddress() { CustomerId = customer.Id, Customer = customer, StreetAddress = "100 Main St.", CreatedAt = new DateTimeOffset(DateTime.Now)});
customer.Addresses.Add(new CustomerAddress() { CustomerId = customer.Id, Customer = customer, StreetAddress = "101 Main St.", CreatedAt = new DateTimeOffset(DateTime.Now)});
customer.Addresses.Add(new CustomerAddress() { CustomerId = customer.Id, Customer = customer, StreetAddress = "102 Main St.", CreatedAt = new DateTimeOffset(DateTime.Now)});
context.SaveChanges();
增加/保存了客户。
但是,在 second context.SaveChanges()
上,我收到一个SqlException错误,提示:
无效的列名“有效”。
无效的列名“ StreetAddress”。
无效的列名“ Id”。
无效的列名“ Id”。
此外,AddressBase
类的所有其余列(此处未显示)。
我在做什么错了?