문제

EF 4.1 코드가 먼저 일종의 "다중 상속"관계를 모델링하려고합니다. 여기에 내가하려고하는 예제는 다음과 같습니다.

사용자가 "사용자"객체를 사용하여 사용자가 응용 프로그램과 상호 작용하는 방식을 모델링하려고합니다. 이것은 기본 클래스 인 이에 따라 특히 홈페이지를 방문하는 것과 같은 아무 것도하지 않을 때 현재 사용자를 설명하는 데 사용됩니다. 다음과 같이 보일 수 있습니다 :

public class User
{
    public Guid ID { get; set; }    // Just use the forms authentication user ID
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
.

이제는 동일한 사용자의 표현을 만들고 싶지만 사이트의 다른 부분에서 구매자로서 다음과 같이 표시 될 수 있습니다.

public class Shopper : User
{
    public virtual ICollection<Orders> Orders { get; set; }
}
.

등등 등. 기존 사용자 항목이있는 구매자를 삽입하려면 PK가 이미 사용자 테이블에서 촬영되어 있기 때문에 예외가 발생합니다.

EF 코드를 먼저 모델링하는 (ISA) 관계를 모델링 할 수있는 방법이 있습니까? 아니면 내가이 같은 것에 붙어있을거야?

public class Shopper
{
    public Guid UserID { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Order> Orders { get; set; }

    public string FirstName
    {
        get { return User.FirstName; }
        set { User.FirstName = value; }
    }

    // yada, yada, yada...
}
.

코드를 먼저 붙이고 내 dbContext에서 바로 관계를 모델링하고 싶지만 이런 일을하는 방법을 알아낼 수는 없습니다. 감사합니다!

편집 :

그래서, 나는 이렇게하려고 노력하고 있습니다 :

public void SomeMethod ()
{
    var UserID = Guid.NewGuid ();
    var MyUser = new User () { ID = UserID };
    SaveUserToDatabase (MyUser);

    var ShopperRepresentation = GetUserAsShopper (UserID);
    //  Do stuff.
}
.

기본적으로 객체 지향 역할을 사용하는 것과 같습니다. 저는 해당 사용자의 모든 replenation에 대해 동일한 PK를 사용하고 싶지만 사용자라는 기본 클래스에 모든 기본 정보를 저장합니다. 물론 내 자신의 SQL을 작성하면 가능한 것이 가능합니다. 그러나 EF 코드가 먼저 수행 할 수 있는지 확인하고 싶습니다.

도움이 되었습니까?

해결책

Yes, you can do it the way you describe in your first two code examples.

I think you just need to define a mapping, which you'll want to do in your OnModelCreating function of your DataContext in addition to having your classes set up right. How you do it depends on what mapping scheme you're using. I went for Table-Per-Type (TPT) in my most recent project, so I had something like this:

modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Shopper>().ToTable("Shoppers");
modelBuilder.Entity<OtherUser>().ToTable("OtherUsers");

Let me know if that doesn't work for you and I'll see what I can do.

Edit:

Having seen your clarification below, I can't think of a way to do that. You'd have to keep each objects stored separately (having EF treat a Shopper as just a Shopper, not a Shopper and a User), even though they share common data. That could lead to data mismatches (if, say, Shopper got its LastName updated but User didn't). I think you might be better off going with something like:

public class User
{
    public virtual Guid ID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual ShopperInfo { get; set; }
}

public class ShopperInfo
{
    public virtual ICollection<Order> Orders { get; set; }
}

and then when you need to treat User as a Shopper, you just access the ShopperInfo (and if its not there, you create it). EF will be able to properly set that up for you no problem.

Though if you're going to have a lot of types of users, that might get cumbersome. Just a suggestion though - I think its a bit cleaner.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top