使用 Fluent API 在 EF6 中自定义类型映射
Custom type mappings in EF6 using Fluent API
我真的不知道如何更好地表达标题,所以对于任何不正确的地方,我深表歉意。
这是我的问题:
我有以下实体:
public sealed class AppUser: DomainEntityBase
{
public bool ExternalLoginsEnabled { get; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public Email Email { get; set; }
public virtual string PasswordHash { get; set; }
public virtual string SecurityStamp { get; set; }
}
和
public sealed class Email
{
public string EmailAddress { get; }
public Email(string email)
{
try
{
EmailAddress = new MailAddress(email).Address;
}
catch (FormatException)
{
//omitted for brevity
}
}
}
我的问题是,在代码级别,我真的希望将电子邮件(以及我没有放在这里的其他几个)视为 类,因为它们将具有验证器等(我试图将其移至适当的 DDD)
但在数据库级别,我只需要将电子邮件存储为字符串。
问题:使用流利的API,我该如何配置这样的关系?
目前我有
public class AppUserConfiguration:EntityConfigurationBase<AppUser>
{
/// <summary>
/// Initializes a new instance of the <see cref="AppUserConfiguration"/> class.
/// </summary>
public AppUserConfiguration()
{
ToTable("AppUser");
Property(x => x.PasswordHash).IsMaxLength().IsOptional();
Property(x => x.ExternalLoginsEnabled).IsRequired();
Property(x => x.SecurityStamp).IsMaxLength().IsOptional();
Property(x => x.Username).HasMaxLength(256).IsRequired();
...
}
}
在您的 OnModelCreating 中,定义您的复杂类型:
protected sealed override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<EMail>()
.Property(t => t.EmailAddress)
.HasMaxLength(255);
}
然后在您的配置中:
...
public AppUserConfiguration()
{
ToTable("AppUser");
Property(x => x.PasswordHash).IsMaxLength().IsOptional();
Property(x => x.ExternalLoginsEnabled).IsRequired();
Property(x => x.SecurityStamp).IsMaxLength().IsOptional();
Property(x => x.Email.EMailAddress).IsOptional();
Property(x => x.Username).HasMaxLength(256).IsRequired();
...
}
我真的不知道如何更好地表达标题,所以对于任何不正确的地方,我深表歉意。 这是我的问题:
我有以下实体:
public sealed class AppUser: DomainEntityBase
{
public bool ExternalLoginsEnabled { get; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public Email Email { get; set; }
public virtual string PasswordHash { get; set; }
public virtual string SecurityStamp { get; set; }
}
和
public sealed class Email
{
public string EmailAddress { get; }
public Email(string email)
{
try
{
EmailAddress = new MailAddress(email).Address;
}
catch (FormatException)
{
//omitted for brevity
}
}
}
我的问题是,在代码级别,我真的希望将电子邮件(以及我没有放在这里的其他几个)视为 类,因为它们将具有验证器等(我试图将其移至适当的 DDD) 但在数据库级别,我只需要将电子邮件存储为字符串。
问题:使用流利的API,我该如何配置这样的关系?
目前我有
public class AppUserConfiguration:EntityConfigurationBase<AppUser>
{
/// <summary>
/// Initializes a new instance of the <see cref="AppUserConfiguration"/> class.
/// </summary>
public AppUserConfiguration()
{
ToTable("AppUser");
Property(x => x.PasswordHash).IsMaxLength().IsOptional();
Property(x => x.ExternalLoginsEnabled).IsRequired();
Property(x => x.SecurityStamp).IsMaxLength().IsOptional();
Property(x => x.Username).HasMaxLength(256).IsRequired();
...
}
}
在您的 OnModelCreating 中,定义您的复杂类型:
protected sealed override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<EMail>()
.Property(t => t.EmailAddress)
.HasMaxLength(255);
}
然后在您的配置中:
...
public AppUserConfiguration()
{
ToTable("AppUser");
Property(x => x.PasswordHash).IsMaxLength().IsOptional();
Property(x => x.ExternalLoginsEnabled).IsRequired();
Property(x => x.SecurityStamp).IsMaxLength().IsOptional();
Property(x => x.Email.EMailAddress).IsOptional();
Property(x => x.Username).HasMaxLength(256).IsRequired();
...
}