使用 EF 4 POCO 将自定义类绑定到数据库中的列

发布于 2024-10-19 02:51:56 字数 1919 浏览 1 评论 0原文

我创建了一个类,我们称之为 User。在这个类中,我有一个自定义创建的类,称为电子邮件。此类仅包含一个保存电子邮件地址值的字符串和一些验证地址的逻辑。所以在我的 User 类中看起来像这样。

public class User{
 public string Name{get;set;}
 public EMailAddress EMail{get;set;}
 ...
}

我现在想使用 EF4 的 CTP5 代码将此电子邮件绑定到我的数据库中的列。但我不能这样做,我什至没有得到一个好的异常,我得到的只是“线程中止异常”,但如果我注释掉我的电子邮件属性,它会很好地工作。

我的 EMailAddress 类如下所示。

public class EMailAddress
{
    //-- Declaration
    private string _email;

    //-- Constructor
    public EMailAddress(string emailAddress)
    {
        if (emailAddress == null)
            throw new ArgumentNullException(string.Format("Supplied emailaddress can't be null"));
        if (!IsValid(emailAddress))
            throw new ArgumentException(string.Format("'{0}' is not a valid Emailaddress", emailAddress));

       _email = emailAddress;
   }

   //-- Methods
   private static bool IsValid(string emailAddress)
   {
       Regex re = new Regex(Constants.EMAIL_REGULAR_EXPRESSION_PATTERN);
       return re.IsMatch(emailAddress);
   }

   public override string ToString()
   {
       return _email;
   }

   public override bool Equals(object obj)
   {
       if (obj == null)
           return false;

       if (obj is string)
           return _email == (string)obj;

       if(obj is EMailAddress)
           return _email == ((EMailAddress)obj).ToString();

       return false;
   }

   public override int GetHashCode()
   {
       return _email.GetHashCode();
   }

   //-- Operator
   public static bool operator ==(EMailAddress emailAddress, EMailAddress emailAddress2)
   {
       return object.Equals(emailAddress, emailAddress2);
   }

   public static bool operator !=(EMailAddress emailAddress, EMailAddress emailAddress2)
   {
       return !(emailAddress == emailAddress2);
   }
}

我想让我的 EMailAddress 类不包含任何公共属性。有没有办法让 EF 在将值保存到数据库时使用 .ToString() 方法,并在从数据库加载数据来填充我的对象时使用构造函数。

谢谢...

I have create a class, let's call it User. In this class I have a custom created class called EMail. This class contains only a string that holds the value of the emailadress and some logic to verify the address. So it looks like this in my User class.

public class User{
 public string Name{get;set;}
 public EMailAddress EMail{get;set;}
 ...
}

I now want to bind this EMail to a column in my databas by using EF4's CTP5 code. But I can't do this, I don't even get an good exception back, all I get is "Thread aborted exception", but if I comment out my EMail property it works good.

My EMailAddress class looks like this.

public class EMailAddress
{
    //-- Declaration
    private string _email;

    //-- Constructor
    public EMailAddress(string emailAddress)
    {
        if (emailAddress == null)
            throw new ArgumentNullException(string.Format("Supplied emailaddress can't be null"));
        if (!IsValid(emailAddress))
            throw new ArgumentException(string.Format("'{0}' is not a valid Emailaddress", emailAddress));

       _email = emailAddress;
   }

   //-- Methods
   private static bool IsValid(string emailAddress)
   {
       Regex re = new Regex(Constants.EMAIL_REGULAR_EXPRESSION_PATTERN);
       return re.IsMatch(emailAddress);
   }

   public override string ToString()
   {
       return _email;
   }

   public override bool Equals(object obj)
   {
       if (obj == null)
           return false;

       if (obj is string)
           return _email == (string)obj;

       if(obj is EMailAddress)
           return _email == ((EMailAddress)obj).ToString();

       return false;
   }

   public override int GetHashCode()
   {
       return _email.GetHashCode();
   }

   //-- Operator
   public static bool operator ==(EMailAddress emailAddress, EMailAddress emailAddress2)
   {
       return object.Equals(emailAddress, emailAddress2);
   }

   public static bool operator !=(EMailAddress emailAddress, EMailAddress emailAddress2)
   {
       return !(emailAddress == emailAddress2);
   }
}

And I want to keep my EMailAddress class free of any public properties. Is there a way to let the EF use the .ToString() method when itsaves the value to the database, and use the constructor when loading the data from the database to populate my objects.

Thanks...

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

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

发布评论

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

评论(2

海的爱人是光 2024-10-26 02:51:57

不,这是不可能的。您有两种选择:

  • 将另一个字符串属性添加到您的 User 类中。该属性将负责返回电子邮件和设置电子邮件(创建 EmailAddress 实例)。该属性将被映射。将 [NotMappedAttribute] 添加到 EMail 属性。您可以利用新属性的可见性。在常见的 EF 中,您可以更改属性的可见性,但我不确定在代码优先中是否也可以。
  • 将 EMailAddress 映射为复杂类型(用 [ComplexTypeAttribute] 标记它,但在这种情况下,您再次需要向 EMailAddress 添加字符串属性。

No it is not possible. You have two choices:

  • Add another string property to your User class. This property will be responsible for returning email and setting email (creating EmailAddress instance). This property will be mapped. Add [NotMappedAttribute] to EMail property. You can play with visibility of the new property. In common EF you can change visibility of property but I'm not sure if it is also possible in code-first.
  • Map EMailAddress as complex type (mark it with [ComplexTypeAttribute] but in such case you again need to add string property to EMailAddress.
软糖 2024-10-26 02:51:57

您可以将非公共属性映射到 EF 中的列,但默认的 codefirst API 不支持开箱即用。我提供了一些免费代码,您可以在项目中使用它们来支持这一需求。您仍然需要拥有这些属性,但它们现在可以是受保护的、私有的或内部的。

详细信息在这里
http ://blogs.msdn.com/b/schlepticons/archive/2011/08/31/map-private-properties-with-ef- Fluent-api.aspx

You can map non-public properties to columns in EF but the default codefirst API doesn't support it out of the box. I've made some free code available that you can use in your projects to support this need. You'll still have to have the properties but they can be protected or private or internal now.

Details are here
http://blogs.msdn.com/b/schlepticons/archive/2011/08/31/map-private-properties-with-ef-fluent-api.aspx

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