나는 EF4 코드만으로 무언가를 찾으려고합니다. TPH를 사용하여 저장된 Person을 Instructor로 변경하거나 그 반대로 변경하려는 경우 어떻게해야합니까? 내 POCO 클래스 : 내가이 사람 관리자를 만들고 싶어에EF4 코드 만 개체의 TPH 변경 유형
var person1 = new Person { FirstName = "Bayram", LastName = "Celik" };
context.People.Add(person1);
context.SaveChanges();
나중에 :
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Instructor : Person
{
public DateTime? HireDate { get; set; }
}
public class Student : Person
{
public DateTime? EnrollmentDate { get; set; }
}
public class Admin : Person
{
public DateTime? AdminDate { get; set; }
}
public class PersonConfiguration : EntityConfiguration<Person>
{
public PersonConfiguration()
{
this.HasKey(u => u.PersonId).Property(u => u.PersonId).IsIdentity();
MapHierarchy()
.Case<Person>(p => new
{
p.PersonId,
p.FirstName,
p.LastName,
PersonCategory = 0
})
.Case<Instructor>(i => new
{
i.HireDate,
PersonCategory = 1
})
.Case<Student>(s => new
{
s.EnrollmentDate,
PersonCategory = 2
})
.Case<Admin>(a => new
{
a.AdminDate,
PersonCategory = 3
}).ToTable("Person");
}
}
난 사람이 있다고 할 수 있습니다. 내가 어떻게 이것을 달성 할 수 있을까?
var person = context.People.FirstOrDefault();
Admin test = person as Admin; // wont work
HIREDATE 열을 변경합니다 다음 그러나 나의 판별 필드 PersonCategory는 그래서 EF는
Admin admin = new Admin();
admin.PersonId = person.PersonId;
admin.AdminDate = DateTime.Now;
context.ObjectContext.Detach(person);
context.People.Attach(admin);
var customerEntry = context.ObjectContext.ObjectStateManager.GetObjectStateEntry(admin);
customerEntry.SetModified();
customerEntry.SetModifiedProperty("AdminDate");
감사합니다. 모든 프레임 워크에서 코드를 가져 왔습니다. http://1code.codeplex.com/. 그리고 내가 시도한 것을 시도하려는 생각에 저항 할 수 없었습니다. – Bcelik