我已经无数次看到这个错误,原因总是由于外键设置错误。但在这种情况下,我只是看不到问题。

本次测试:

    [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));

    }

抛出这个异常:



这是我正在为其构建模型的 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);

仅有的两个相关实体是 MemberEmployee ,它们具有 ICollection ,如下所示:
// Member.cs
public virtual ICollection<Note> Notes { get; set; }

// Employee.cs
public virtual ICollection<Members.Note> EnteredMemberNotes { get; set; }

EF 查询来自测试的输出( _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 来自哪里?我不知所措。谢谢

最佳答案

找到了。

我的 Member.cs 类有一个公共(public)只读属性,它根据 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);

感谢您的关注,希望有一天这能帮助其他一些可怜的草皮。

关于c# - EF6 无效的列名 Member_ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41729421/

10-17 00:15