我几天后就面临这个问题而且无法理解。
我尝试通过将此json代码发布到我的API来创建新客户,并抛出我的错误: “无效的列名'ContactId'。无效的列名'ContactId1'。”
{
"sameAsCompanyAddress": true,
"billingAddress": { line1: "A", suburb: "A", state: "A", postcode: "A" },
"companyAddress": { line1: "C", suburb: "C", state: "C", postcode: "C" },
"companyContacts": [
{ firstName: "A", lastName: "A", position: "A", mobile: "A", phone: "A", email: "A" },
{ firstName: "B", lastName: "B", position: "B", mobile: "B", phone: "B", email: "B" }
],
"complianceDocuments": [],
"keyContact": { firstName: "C", lastName: "C", position: "C", mobile: "C", phone: "C", email: "C" },
"rating": 1,
"sameAsCompanyAddress": true,
"tradeName": "C"
}
我认为这是一个外键问题,但无法找到我错过的那个......
我的数据库表是这样的:
我的上下文是这样的:
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.Abn).HasColumnType("char(11)");
entity.Property(e => e.Acn).HasColumnType("char(9)");
entity.Property(e => e.CreditLimit).HasColumnType("money");
entity.Property(e => e.Rating).HasDefaultValueSql("((0))");
entity.Property(e => e.SameAsCompanyAddress).HasDefaultValueSql("((1))");
entity.HasOne(d => d.BillingAddress)
.WithMany(p => p.CustomerBillingAddress)
.HasForeignKey(d => d.BillingAddressId)
.HasConstraintName("FK_Customer_Address1");
entity.HasOne(d => d.CompanyAddress)
.WithMany(p => p.CustomerCompanyAddress)
.HasForeignKey(d => d.CompanyAddressId)
.HasConstraintName("FK_Customer_Address");
entity.HasOne(d => d.KeyContact)
.WithMany(p => p.CustomerNavigation)
.HasForeignKey(d => d.KeyContactId)
.HasConstraintName("FK_Customer_CompanyContact");
entity.HasOne(d => d.PrimaryContact)
.WithMany(p => p.Customer)
.HasForeignKey(d => d.PrimaryContactId)
.HasConstraintName("FK_Customer_CustomerContact");
});
modelBuilder.Entity<CustomerContact>(entity =>
{
entity.HasOne(d => d.CustomerNavigation)
.WithMany(p => p.CustomerContact)
.HasForeignKey(d => d.CustomerId)
.HasConstraintName("FK_CustomerContact_Customer");
});
modelBuilder.Entity<CompanyContact>(entity =>
{
entity.Property(e => e.Phone).IsUnicode(false);
entity.HasOne(d => d.Subcontractor)
.WithMany(p => p.CompanyContact)
.HasForeignKey(d => d.SubcontractorId)
.HasConstraintName("FK_KeyContact_Subcontractor");
entity.HasOne(d => d.Supplier)
.WithMany(p => p.CompanyContact)
.HasForeignKey(d => d.SupplierId)
.HasConstraintName("FK_KeyContact_Supplier");
entity.HasOne(d => d.Customer)
.WithMany(p => p.CompanyContact)
.HasForeignKey(d => d.CustomerId)
.HasConstraintName("FK_KeyContact_Customer");
});
感谢您的帮助:)
编辑:
以下是生成的查询:
INSERT INTO [Customer] ([Abn], [AccountStatusId], [Acn], [ArchivalDate], [BillingAddressId], [CompanyAddressId], [ContactId], [ContactId1], [CreditApplicationReceived], [CreditLimit], [KeyContactId], [LegalEntity], [PrimaryContactId], [PurchaseOrderRequired], [Rating], [RegisteredDate], [TradeName])
VALUES (@p18, @p19, @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28, @p29, @p30, @p31, @p32, @p33, @p34);
SELECT [Id], [SameAsCompanyAddress]
FROM [Customer]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ContactId'. Invalid column name 'ContactId1'.