如何首先通过设置忽略实体框架代码中的实体

发布于 2024-12-11 18:26:17 字数 1056 浏览 0 评论 0原文

假设我有 4 个类:

public class Component {}
public class SubComponent1 : Component {}
public class SubComponent2 : Component {}
public class Container : Component { 
  public List<Component> components { get; set;}
}

并且我不想将 SubComponent2 存储在数据库中。我想在整个应用程序中像普通 POCO 一样使用它,只希望在尝试保存到数据库时忽略它。 到目前为止,我尝试的是将 Ignore 放在我的 DbContext OnModelCreating 方法中:

dbModelBuilder.Ignore<SubComponent2>();

然后尝试在数据库中保存一些还包含 SubComponent2 的结构,希望 SubComponent2 会被忽略,从而不会被保存。 所以基本上是这样的:

var someContainer = new Container{ components = new List<Component>{ 
  new SubComponent1(), new SubComponent2() 
};
context.Containers.Add(someContainer);

并希望只有带有 SubComponent1 的容器才能访问数据库。 我得到的是

找不到 EntityType“SubComponent2”的映射和元数​​据信息

我知道我可以简单地手动删除所有 SubComponent2-s,然后将 Container 添加到上下文,保存并以某种方式重新附加 SubComponent2-s,但这对于较大的结构来说似乎开销太大当我希望实体框架能够“为我”解决这个问题时,

您知道解决这个问题的任何好方法吗(可能是我的 DbContext 中的 dbModelBuilder 设置发生了一些变化)?

Lets say i have 4 classes:

public class Component {}
public class SubComponent1 : Component {}
public class SubComponent2 : Component {}
public class Container : Component { 
  public List<Component> components { get; set;}
}

And I don't want to store SubComponent2 on database. I want to use it throughout application as normal POCO, only want it to be ignored when trying to save to database.
What I have tried so far was to put Ignore for it in my DbContext OnModelCreating method:

dbModelBuilder.Ignore<SubComponent2>();

and then tried to save some structure containing also SubComponent2 on database hoping that SubComponent2 would simply be ignored, and thus not saved.
So basically something like:

var someContainer = new Container{ components = new List<Component>{ 
  new SubComponent1(), new SubComponent2() 
};
context.Containers.Add(someContainer);

and hoped only Container with SubComponent1 would get to database.
What I got instead was

Mapping and metadata information could not be found for EntityType 'SubComponent2'

I know I could simply manually remove all SubComponent2-s, then add Container to context, save and then reattach SubComponent2-s somehow, but that seems like too much overhead on bigger structures when I hoped entity framework could be able to solve this "for me"

Do you know of any nice way around this (probably some change in dbModelBuilder setup in my DbContext) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-12-18 18:26:17

我认为目前这是不受支持的。您可以忽略派生实体,因此它不会被映射,但在保存期间不能在导航属性中使用它。我没有测试它,但我几乎可以肯定 EF 没有逻辑来跳过继承映射中被忽略的实体,因为被忽略的实体根本不属于持久化集合。

I think this is unsupported at the moment. You can ignore derived entity so it is not mapped but you cannot use it in navigation properties during saving. I didn't test it but I'm almost sure that EF has no logic to skip ignored entities in inheritance mapping because ignored entity simply doesn't belong to persisted collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文