简体   繁体   English

实体框架自定义字段持久性

[英]Entity Framework Custom field persistence

I am using the Entity Framework with a database first approach. 我将实体框架与数据库优先方法结合使用。

I have used partial classes to successfully add custom methods, but have found that if I add a custom field it is not persisted when my entity is next pulled from the DataContext (entity is not submitted to database in the meantime). 我已经使用局部类成功添加了自定义方法,但是发现如果我添加了一个自定义字段,则在下一次从DataContext提取我的实体时,该字段不会DataContext (同时实体未提交到数据库)。 I need to use this field for processing but I do not need it, or want it, stored in my database. 我需要使用该字段进行处理,但不需要或不需要将其存储在数据库中。

public partial class MyEntity
{           
    public decimal MyCustomField;
}

Why is this? 为什么是这样? How can I get around it? 我该如何解决?

Thanks 谢谢

One thing you should keep in mind is that Entity Framework do not store values in itself, it just generates SQL queries using its knowledge of database from EDMX. 您应该牢记的一件事是,Entity Framework本身并不存储值,它只是利用对EDMX的数据库知识来生成SQL查询。 Persistence is actually implemented at the database side. 持久性实际上是在数据库端实现的。

Yes, you can add custom methods and properties to model using partial classes, but since, you are not creating a database column, the values in those properties will not be persisted and hence the behavior you are getting is perfectly normal. 是的,您可以使用部分类添加自定义方法和属性以进行建模,但是由于您没有创建数据库列,因此这些属性中的值将不会保留,因此您得到的行为是完全正常的。

I hope this helps. 我希望这有帮助。

尝试使用“属性”而不是“字段”。

public decimal MyCustomField{get; set;}

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

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