问题描述
例如
$
可以在EF Code First方法中覆盖或添加实体模型对象属性设置的代码。 b $ b
public class联系
{
[Key]
public int Id {get;私人集合}
public string FirstName {get;组; }
public string LastName {get;组; }
public string JobTitle
{
get;
set {// eg。正确的情况下的职位}
}
}
已尝试将公共属性标记为NotMapped,并将此set / get设置为private / protected属性。但是,似乎该属性必须在表中创建。
如果您想要,可以在其中写入逻辑,只需将该属性转换为非自动属性,然后按照正常属性执行检查。
私有字符串职称;
$请记住,如果您从设置器中更改db的值,则可能会在执行
public string JobTitle
{
get {return jobTitle; }
set
{
//做你喜欢的东西或只是jobTitle =值
}
}
SaveChanges()
在上下文中。Is it possible to override or add code to setter of property for entity model object in EF Code First approach.
e.g.
public class Contact { [Key] public int Id { get; private set; } public string FirstName { get; set; } public string LastName { get; set; } public string JobTitle { get; set { // eg. proper case the job title } } }
I had tried having a public property marked NotMapped and this set/get to private/protected property that is. But it seems that the property has to public to be created in table.
解决方案You can write the logic in there if you want, just transform the property into a non-automatic one and perform the checks as you would do with a normal property.
private string jobTitle; public string JobTitle { get { return jobTitle; } set { // do your fancy stuff or just jobTitle = value } }
Remember that if you change the value from db inside your setter it will probably be saved that way later on after you perform
SaveChanges()
on the context.这篇关于为EF Code First模型实体提供自定义Setter on属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!