gpt4 book ai didi

asp.net-mvc - Entity Framework 6 - 如何将属性标记为非外键

转载 作者:行者123 更新时间:2023-12-05 01:24:38 26 4
gpt4 key购买 nike

我将 ASP.NET MVC5 与 EF6 结合使用,并使用代码优先方法。

我在模型中有一个属性,我需要告诉 EF6 不是外键:

public class LogEntry
{
public int ID { get; set; }
public int LogDayID { get; set; }
public int LogEntryTypeID { get; set; }
public int DepartmentID { get; set; }
public DateTime Clock { get; set; }
public string Text { get; set; }

public virtual LogDay LogDay { get; set; }
public virtual LogEntryType LogEntryType { get; set; }
public virtual Department Department { get; set; }
}

[NotMapped]
public class Department
{
public int ID { get; set; }
public string Title { get; set; }
}

模型部门有 [NotMapped],因为该模型不应存储在数据库中。

我认为这足以让 EF6 意识到 LogEntry 中的 DepartmentID 不应该是外键。但它却抛出一个错误,指出“部门”未映射。

编辑:即使我从 LogEntry 中删除 DepartmentID,它仍然会提示上述错误。

完整的错误信息如下:

"The type 'SupervisorLogWeb.Models.Department' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject."

最佳答案

显然,您的 ComplexType 被发现为实体 - 如果您决定将以前的实体重构为 ComplexType,就会发生这种情况。

ModelBuilder 将根据类型是否存在于 DbContext 中来决定它是否(或多或少)是实体。

因此请检查您的类是否仍在 Context 中定义为 DbSet 并进行相应调整。

关于asp.net-mvc - Entity Framework 6 - 如何将属性标记为非外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28026246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com