我已经看过这个错误了很多次,原因总是由于外键设置配置错误造成的。但在这种情况下,我无法看到问题。
这个测试:
[TestMethod]
[TestCategory("Integration")]
public void DataModelMemberNoteBuilds() {
_context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
var sut = _context.MemberNotes.FirstOrDefault();
if (sut == null) {
Assert.Inconclusive("return null");
}
Assert.IsInstanceOfType(sut, typeof(Domain.Members.Note));
}
引发此异常:
System.Data.SqlClient.SqlException:列名无效' Member_ID'。
这是POCO我正在构建模型:
public class Note
{
public int ID { get; set; }
public int MemberID { get; set; }
public DateTime Timestamp { get; set; }
public int EnteredByEmployeeID { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public bool IsActiveHotnote { get; set; }
public virtual Member Member { get; set; }
public virtual Employees.Employee EnteredByEmployee { get; set; }
}
表格(不是代码优先生成):
CREATE TABLE [dbo].[MemberNotes](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DateCreated] [datetime2](7) NOT NULL,
[rv] [timestamp] NOT NULL,
[MemberID] [int] NOT NULL,
[NoteTimestamp] [datetime2](7) NOT NULL,
[NoteEnteredByEmployeeID] [int] NOT NULL,
[NoteSubject] [nvarchar](255) NULL,
[NoteBody] [nvarchar](2000) NOT NULL,
[NoteIsActiveHotnote] [bit] NOT NULL,
PRIMARY KEY CLUSTERED ([ID] 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].[MemberNotes] ADD DEFAULT (getdate()) FOR [DateCreated]
GO
ALTER TABLE [dbo].[MemberNotes] ADD DEFAULT (getdate()) FOR [NoteTimestamp]
GO
ALTER TABLE [dbo].[MemberNotes] ADD DEFAULT ((0)) FOR [NoteIsActiveHotnote]
GO
ALTER TABLE [dbo].[MemberNotes] WITH CHECK ADD FOREIGN KEY([MemberID])
REFERENCES [dbo].[Members] ([MemberID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[MemberNotes] WITH CHECK ADD FOREIGN KEY([NoteEnteredByEmployeeID])
REFERENCES [dbo].[Employees] ([EmployeeID])
ON UPDATE CASCADE
-- <<<<<<<<<<<<<< NOTE ON DELETE NO ACTION <<<<<<<<<<<<<<<<<
GO
正在构建模型:
mb.Entity<Note>().ToTable("MemberNotes");
mb.Entity<Note>().Property(x => x.Body).HasColumnName("NoteBody");
mb.Entity<Note>().Property(x => x.EnteredByEmployeeID).HasColumnName("NoteEnteredByEmployeeID");
mb.Entity<Note>().Property(x => x.IsActiveHotnote).HasColumnName("NoteIsActiveHotnote");
mb.Entity<Note>().Property(x => x.Subject).HasColumnName("NoteSubject");
mb.Entity<Note>().Property(x => x.Timestamp).HasColumnName("NoteTimestamp");
mb.Entity<Note>()
.HasRequired(x => x.Member)
.WithMany(x => x.Notes)
.HasForeignKey(x => x.MemberID);
mb.Entity<Note>()
.HasRequired(x => x.EnteredByEmployee)
.WithMany(x => x.EnteredMemberNotes)
.HasForeignKey(x => x.EnteredByEmployeeID);
唯一的两个相关实体是Member
和Employee
,其中ICollection
为:{/ p>
// Member.cs
public virtual ICollection<Note> Notes { get; set; }
// Employee.cs
public virtual ICollection<Members.Note> EnteredMemberNotes { get; set; }
来自测试的EF Query输出(_context.MembersNotes.FirstOrDefault()
):
SELECT TOP (1)
[c].[ID] AS [ID],
[c].[MemberID] AS [MemberID],
[c].[NoteTimestamp] AS [NoteTimestamp],
[c].[NoteEnteredByEmployeeID] AS [NoteEnteredByEmployeeID],
[c].[NoteSubject] AS [NoteSubject],
[c].[NoteBody] AS [NoteBody],
[c].[NoteIsActiveHotnote] AS [NoteIsActiveHotnote],
[c].[Member_ID] AS [Member_ID]
FROM [dbo].[MemberNotes] AS [c]
任何人都可以看到Member_ID
的来源吗?我不知所措。感谢
答案 0 :(得分:1)
找到它。
我的Member.cs类有一个公共只读属性,根据List<Note>
中的过滤内容返回ICollection<Note>
public List<Note> Hotnotes {
get
{
if (Notes == null) {
return new List<Note>();
}
return Notes.Where(x => x.IsActiveHotnote).OrderByDescending(x => x.Timestamp).ToList();
}
}
我忽略了告诉EF忽略这个属性。它正在读取类型并将该属性作为附加关系/导航属性。
将其放入模型构建器可以解决问题:
mb.Entity<Member>().Ignore(x => x.Hotnotes);
感谢您的关注,希望有一天能帮助其他一些可怜的人。