2
EntityFramework CodeFirst 속성에 대한 getter/setter에 논리를 추가 할 수 있습니까?EF CodeFirst 속성에 대한 getter/setter를 재정의 할 수 있습니까?
예를 들어, 내가 좋아하는 일을하고 싶습니다 다음 Dog.Name 속성을 반환하기 전에 조금 처리 할 수
public class Dog {
// This is a normal EF-backed property....
public virtual string Breed { get; set; }
// But we'd like a little logic applied to the Name!
public virtual string Name {
get {
string nameInDB = base.Name;
// where base.Name would be the same as the naked "get;" accessor
if (nameInDB == "Fido") {
return "Fido the Third"
} else {
return nameInDB;
}
}
set;
}
}
public class PetContext : DbContext {
public DbSet<Dog> Dogs;
}
.
EF CodeFirst가 사적 속성을 매핑 할 수 있다는 것을 몰랐습니다. – Seth
시도하기 전에 EF 매핑 개인 속성을 찾으십시오. Linq에서 엔티티로 쉽게이 작업을 수행 할 수 있습니다. https://msdn.microsoft.com/en-us/library/system.data.linq.mapping.dataattribute.storage(v=vs.110).aspx # Anchor_2 System.ComponentModel.DataAnnotations를 사용하는 경우이를 지원하는 코드를 추가해야합니다. –