[英]Entity Framework Code First Saving type as string
I am using Entity Framework Code First and have some Classes with a System.Net.Mail.MailAddress type like this. 我正在使用Entity Framework Code First,并且有一些像这样的System.Net.Mail.MailAddress类的Classes。
public class Person
{
public MailAddress Email { get; set; }
}
When EF tries to create the DB I get this error 当EF尝试创建数据库时,我收到此错误
"Problem in mapping fragments starting at line 6 No mapping for properties Person.Email"
How can I tell EF to just save the result of the Email.ToString() to the DB and then I can make a new MailAddress(stringOfEmail); 如何告诉EF只将Email.ToString()的结果保存到数据库,然后我可以创建一个新的MailAddress(stringOfEmail); when setting the value on the object?
在对象上设置值时?
Or am I better off just changing he data type for Email to a string and handle the validation somewhere else? 或者我最好只将电子邮件的数据类型更改为字符串并在其他地方处理验证?
One possible way would be to have the MailAddress
property not mapped to the DB, but have a 2nd property that is. 一种可能的方法是将
MailAddress
属性映射到数据库,但具有第二个属性。 something like: 就像是:
public class Person
{
public string EmailString { get; set; }
[NotMapped]
public MailAddress Email
{
get { return new MailAddress(this.EmailString); }
set { this.EmailString = value.ToString(); }
}
}
See here for info on the [NotMapped]
attribute: How not persist property EF4 code first? 有关
[NotMapped]
属性的信息,请参见此处: 如何不首先保留属性EF4代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.