简体   繁体   中英

How to add two different properties of the same class to one POCO class?

I have a class that has two references to one class (User):

public class Xpto {
    public string Username { get; set; }
    public virtual User User { get; set; }
    public string Username2 { get; set; }
    public virtual User User2 { get; set; }
}

The thing is EF only creates references to the first key (Username). That way User and User2 have Username as key and not what I intended...

I found this to be the answer:

nHibernate, mapping two properties to the same class

But I wouldn't know how to apply this to my scenario.

Thanks.

EDIT : Folks, nevermind... I guess I should've looked a little further. The answer is here: How do I create a POCO object that has 2 references to another class

The standard is <property_name><key_name> So the correct way would be UserUsername and User2Username

Thanks.

Consider using the ForeignKeyAttribute instead, then you can select the names you like for your key attributes.

public class Xpto {

    [ForeignKey("User")]
    public string Username { get; set; }

    public virtual User User { get; set; }

    [ForeignKey("User2")]
    public string Username2 { get; set; }

    public virtual User User2 { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM