简体   繁体   English

使用Entity Framework是否可以创建未映射到数据库表的实体?

[英]With Entity Framework Is it possible to create an entity that is not mapped on a table of my database?

实体框架:是否可以创建未映射到数据库表的实体?

In such case it is not an entity but just a class = create normal class file and you are done. 在这种情况下,它不是实体,而只是一个类=创建普通的类文件,就可以完成了。 That class cannot be part of your entity model (EDMX) or code first mapping because it is not related to persistence (and EF). 该类不能成为您的实体模型(EDMX)或代码优先映射的一部分,因为它与持久性(和EF)无关。

您可以使用[NotMapped]属性,并且仍在DbContext中对其进行跟踪。

You can create a Class in the EF or create a class in seperate class ClassName.cs and use it as your model. 您可以在EF中创建一个类,也可以在单独的类ClassName.cs创建一个类,并将其用作模型。

Better to Use the Second option, since i will be more clear and if EF grows large, it will be hard to find your class from there and modify. 最好使用Second选项,因为我会更清楚,并且如果EF变大,则很难从那里找到您的班级并进行修改。

您可以使用“复杂类型”。

The Entity Framework is really about auto-generating classes that have all the guts to be easily used in Web Services and connected to Databases. 实体框架实际上是关于自动生成的类,这些类具有易于在Web服务中使用并连接到数据库的所有胆量。 If you don't need those capabilities of EF, then all you are really doing is using a UI to make your Entity Classes, rather than just coding them out by hand. 如果您不需要EF的那些功能,那么您真正要做的就是使用UI来创建您的实体类,而不仅仅是手工编写它们。 It's certainly possible, but you don't gain much by doing so and end up with a lot of auto-generated bloat in your code base. 当然是有可能的,但是这样做并不会带来太多好处,并且在代码库中最终导致大量自动生成的膨胀。

Indeed, since EF does it "a certain way" you lose some flexibility that is otherwise available to you by defining your own Entity Classes in source. 确实,由于EF以“某种方式”执行EF,因此您失去了一些灵活性,而这些灵活性通过在源代码中定义自己的实体类而无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM