在实体 POCO 中使用自定义数据类型?
我有一个业务对象,例如用户
。 User
有一个 PhoneNumber
字段,该字段由字符串表示。由于最好留给我的原因,我创建了一个具有隐式 to/from 字符串运算符的 PhoneNumber
类。我是否需要执行任何特殊操作才能将 PhoneNumber
作为字符串写入数据库?现在,Entity 决定将我的 PhoneNumber
类分解为其组成部分(AreaCode
、Prefix
等)并将它们保存到数据库中单独。 PhoneNumber
存储在单独的程序集中。
public class PhoneNumber : IEquatable<PhoneNumber>, IComparable<PhoneNumber>
{
public static implicit operator string (PhoneNumber ph)
{
return ph.ToString ();
}
public static implicit operator PhoneNumber (string number)
{
return Parse(number);
}
public static PhoneNumber Parse(string number)
{
// ...
}
public override string ToString ()
{
// produce a Parse-compatible output
}
}
public class User
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual PhoneNumber Phone { get; set; }
}
public class MyContext : DbContext
{
public DbSet<User> Users { get; set; }
}
I have a business object, say a User
. A User
has a PhoneNumber
field, which is represented by a string. I, for reasons best left to me, have created a PhoneNumber
class that has implicit to/from string operators. Do I have to do anything special to get the PhoneNumber
written as a string to the database? Right now, Entity has decided to break my PhoneNumber
class up into its constituent parts (AreaCode
, Prefix
, etc) and save those to the database individually. PhoneNumber
is stored in a separate assembly.
public class PhoneNumber : IEquatable<PhoneNumber>, IComparable<PhoneNumber>
{
public static implicit operator string (PhoneNumber ph)
{
return ph.ToString ();
}
public static implicit operator PhoneNumber (string number)
{
return Parse(number);
}
public static PhoneNumber Parse(string number)
{
// ...
}
public override string ToString ()
{
// produce a Parse-compatible output
}
}
public class User
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual PhoneNumber Phone { get; set; }
}
public class MyContext : DbContext
{
public DbSet<User> Users { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
如果您的设计的其余部分允许,您可以将 PhoneNumber
类保留在映射之外,并让 User
处理它的字符串表示形式,例如:
public class User
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string PhoneNumber
{
get { return this.PhoneNumber.ToString(); } // TODO: check for null
set { this.PhoneNumber = PhoneNumber.Parse(value); }
}
[NotMapped]
public virtual PhoneNumber Phone { get; set; }
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
唯一的方法是这样的解决方法:
这样您只能在数据库中获得一个
FullNumber
列。The only way is a workaround like this:
This way you would get only a
FullNumber
column in the database.