【发布时间】:2018-03-26 01:41:36
【问题描述】:
在一个 c# 项目中,我有一个 DbContext,它引用了我的域模型的实体。 当我使用
创建迁移代码时添加迁移 MySetup
命令,实体框架(我使用版本 6.1 和 SqlServer)不仅为定义的实体创建迁移,还添加子类的属性(即使它们在不同的命名空间中且未在 DbContext 中使用)。
例子:
class MyContext : DbContext
{
public IDbSet<User> Users { get; set; }
...
}
class User
{
public long Id { get; set; }
...
}
在另一个地方,我有一个没有在上下文中使用的类
class UserExtended : User
{
public string AdditionalPropertyNotForEF { get; set; }
...
}
那么用 add-migration 创建的 Migration 类看起来像
CreateTable(
"dbo.User",
c => new
{
Id = c.Long(nullable: false, identity: true),
AdditionalPropertyNotForEF = c.String()
}) ...
【问题讨论】:
-
在您希望 EF 忽略的属性上使用
[NotMapped]属性 -
@VidmantasBlazevicius 我会使用 NotMapped 属性忽略我的域实体上的特定属性,但不会忽略与上下文无关的完全不同的类。
标签: c# entity-framework-6