实体框架代码优先:如何忽略类

编程入门 行业动态 更新时间:2024-10-23 16:28:40
本文介绍了实体框架代码优先:如何忽略类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这类似于问题此处和此处,但这些文件已过时并且没有任何用处答案.

This is similar to questions here and here, but those are old and have no good answers.

假设我有以下课程:

class HairCutStyle { public int ID { get; set; } public string Name { get; set; } } class CustomerHairCutPreference { public int ID { get; set; } public Customer Customer { get; set; } public HairCutStyle HairCutStyle { get; set; } }

比方说,我的HairCutStyle数据存储在另一个数据库的表中(我从Paul Mitchell自己得到).我想将HairCutStyle类用作POCO类-我将在代码中使用该类来表示理发样式,但无需在数据库中读取/写入该信息. (假设我有一个单独的服务层,可以从另一个数据库填充这些类的数据.)

Let's say my HairCutStyle data is stored in a table in another database (I get it from Paul Mitchell himself). I want to use the HairCutStyle class as a POCO class - something that I will use in code to represent hair cut styles, but I don't need to read/write that information in my database. (Assume I have a separate service layer that can populate the data for these classes from the other database.)

如何告诉EF不要在当前数据库上下文中创建HairCutStyle表?但同时,我想在CustomerHairCutPreference表中存储一个值,该值是对存储在其他位置的HairCutStyle数据的引用.某种虚拟"外键,不受实际数据库FK约束的约束.

How can I tell EF NOT to create a HairCutStyle table in my current db context? But at the same time, I want to store a value in the CustomerHairCutPreference table that is a reference to the HairCutStyle data stored elsewhere. A "virtual" foreign key of sorts, that isn't constrained by an actual database FK constraint.

推荐答案

要确保三件事:

  • 确保您没有在DbContext派生类中公开DbSet<HairCutStyle>
  • 确保您在OnModelCreating替代项中没有提及HairCutStyle
  • 使用NotMapped属性标记您的HairCutStyle属性.
  • Make sure you do not expose a DbSet<HairCutStyle> in your DbContext-derived class
  • Make sure you do not have any mention of HairCutStyle in your OnModelCreating override
  • Mark your HairCutStyle property using the NotMapped attribute.
  • 更多推荐

    实体框架代码优先:如何忽略类

    本文发布于:2023-11-05 09:55:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1560532.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实体   框架   代码

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!