EF 代码优先 CTP5。如何存储来自基类的继承数据
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确实找到了解决我的问题的方法。
I did find the solution to my problem.