我有以下对象模型:
[Table("APA_QuestionProduct")]
public class QuestionProduct
{
[Key, ForeignKey("Question"), Column(Order=0)]
public int QuestionID { get; set; }
[ForeignKey("QuestionID")]
public Question Question { get; set; }
[Key, ForeignKey("Product"), Column(Order=1)]
public int ProductID { get; set; }
[ForeignKey("ProductID")]
public Product Product { get; set; }
}
桌子:
USE [qbm]
GO
/****** Object: Table [dbo].[APA_QuestionProduct] Script Date: 5/21/2013 6:52:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[APA_QuestionProduct](
[QuestionID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
CONSTRAINT [PK_APA_QuestionProduct] PRIMARY KEY CLUSTERED
(
[QuestionID] ASC,
[ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[APA_QuestionProduct] WITH CHECK ADD CONSTRAINT [FK_APA_QuestionProduct_APA_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[APA_Product] ([ProductID])
GO
ALTER TABLE [dbo].[APA_QuestionProduct] CHECK CONSTRAINT [FK_APA_QuestionProduct_APA_Product]
GO
ALTER TABLE [dbo].[APA_QuestionProduct] WITH CHECK ADD CONSTRAINT [FK_APA_QuestionProduct_APA_Question] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[APA_Question] ([QuestionID])
GO
ALTER TABLE [dbo].[APA_QuestionProduct] CHECK CONSTRAINT [FK_APA_QuestionProduct_APA_Question]
GO
所以后面的表只有 2 个外键(也是主键)。我的问题对象有一个产品列表。当我更新 ProductID 外键并在上下文中调用“SaveChanges”时,不会在 db 中生成查询/更新:
question.Products[1].ProductID = 4;
db.Entry(question.Products[1]).State = EntityState.Modified;
db.SaveChanges();
我查看了 InteliTrace 以检查查询,但即使我的 QuestionProduct 对象已更改,也没有对我的 QuestionProduct 表调用查询。为什么表没有更新?没有错误被抛出。