开发者

EF Code First CTP5. How to store the Inheritated data from a baseclas

I have a User class that holds some default data.

   public class User : BaseEntity
    {
        //-- Declaration
        private string _firstname;
        private string _lastname;
        private ICollection<BaseProfile> _profiles = new List<BaseProfile>();

        //-- Constructor
        public User() { }

        //-- Properties
        public string Firstname
        {
            get { return _firstname; }
            set { _firstname = value; base.Name = string.Format("{0} {1}", this.Firstname, this.Lastname); }
        }
        public string Lastname
        {
            get { return _lastname; }
            set { _lastname = value; base.Name = string.Format("{0} {1}", this.Firstname, this.Lastname); }
        }
        public EMailAddress EmailAddress { get; set; }
        public SocialSecurityNumber SocialSecurityNumber { get; set; }
        public Password Password { get; set; }
        public virtual ICollection<BaseProfile> Profiles { get { return _profiles; } set{_profiles = value; }}
    }

The Profiles contains diffrent Profiles data based on diffrent profiles the user can have. If the user is a player, then Profiles will contain of a ProfilePlayer clas, if the user would be a Trainer it will contain a ProfileTrainer class. And if the User is a Player and a Trainer it will contain 2 profiles, one ProfilePlayer Class and one ProfileTrainer class. This profile classes would contain information specified for the diffrent profiles the user could be.

Now to my question, how can I tell the EF that it should save the diffrent BaseProfiles as the specified types, cause when EF is createing the databas for me, it's created as a BaseProfiles开发者_如何学C even if the "real" type is ProfilePlayer class. Do I need to manually create the diffrent tables and then do some mapping or is there a simple way to tell EF to create the diffrent profilesclasses and to save the baseprofiles data into correct table?


I did find the solution to my problem.

    modelBuilder.Entity<Domain.BaseProfile>()
        .Map<Domain.Model.ProfilePlayer>(p => p.Requires("Discriminator").HasValue("ProfilePlayer"))
        .Map<Domain.Model.ProfileTrainer>(p => p.Requires("Discriminator").HasValue("ProfileTrainer"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜