EF 代码优先 CTP5。如何存储来自基类的继承数据

发布于 2024-10-26 20:51:01 字数 1452 浏览 1 评论 0原文

我有一个 User 类,其中包含一些默认数据。

   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; }}
    }

配置文件包含基于用户可以拥有的不同配置文件的不同配置文件数据。如果用户是玩家,则 Profiles 将包含 ProfilePlayer 类,如果用户是 Trainer,则它将包含 ProfileTrainer 类。如果用户是一名球员和一名训练师,它将包含 2 个配置文件,一个 ProfilePlayer 类和一个 ProfileTrainer 类。该配置文件类别将包含为用户可能属于的不同配置文件指定的信息。

现在我的问题是,我如何告诉 EF 应该将不同的 BaseProfiles 保存为指定类型,因为当 EF 为我创建数据库时,即使“真实”类型是 ProfilePlayer 类,它也会创建为 BaseProfiles 。我是否需要手动创建不同的表,然后进行一些映射,或者是否有一种简单的方法来告诉 EF 创建不同的配置文件类并将基本配置文件数据保存到正确的表中?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-11-02 20:51:01

我确实找到了解决我的问题的方法。

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

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"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文