[英]C# Entity Framework 6 Code First: how to define a specific setter for a property stored in DB
I'm using Entity Framework 6 Code First and would to update a value before saved in it DB.我使用的是Entity Framework 6 Code First,并会在保存到数据库之前更新一个值。 I have a model with properties mapped to db table fields and for one of them, MyBackField , I want to trim his value before saving the entity in db via SaveChanges().
我有一个 model 的属性映射到 db 表字段,对于其中一个MyBackField ,我想在通过 SaveChanges() 将实体保存在 db 中之前修剪他的值。 I tried to use backing fields like that:
我尝试使用这样的支持字段:
private string MyBackField;
public string MyFrontField{ get => MyBackField; set => MyBackField = value?.Trim(); }
The thing is: field MyBackField exists already in db, when I change it from public to private property, the Update-Database command generates a migration file to drop it from db.问题是:字段MyBackField已经存在于数据库中,当我将其从公共属性更改为私有属性时,Update-Database 命令会生成一个迁移文件以将其从数据库中删除。
How can I do this?我怎样才能做到这一点?
Finally I switched the member and the property and it worked perfectly.最后我切换了成员和属性,它工作得很好。
Thks @GertArnold谢谢@GertArnold
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.