实体框架5.0关系 [英] Entity Framework 5.0 Relationships

查看:73
本文介绍了实体框架5.0关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Customer 类:

public class Customer : EntityBase<Customer>
{
    public virtual int ID { get; set; }
    public virtual CustomerType CustomerType { get; set; }

    public virtual string CompanyName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    public virtual ICollection<Address> BillingAddresses { get; set; }
    public virtual ICollection<Address> ShippingAddresses { get; set; }
}

地址类看起来像这样:

public class Address : EntityBase<Address>
{
    public virtual int ID { get; set; }
    public virtual AddressType AddressType { get; set; }
    public virtual bool IsDefault { get; set; }
    public virtual string Name { get; set; }
    public virtual string Line1 { get; set; }
    public virtual string Line2 { get; set; }
    public virtual string Line3 { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string ZipCode { get; set; }
    public virtual string Country { get; set; }

    public virtual int CustomerID { get; set; }
    public virtual Customer Customer { get; set; }
}

我希望能够在地址上使用多种地址客户类别,但我很难弄清楚。

I want to be able to have multiple of each kind of address on the customer class, but I am having a hard time figuring that out.

我想最终得到这样的东西,以获取每种类型的默认地址。 / p>

I would like to end up with something like this to get to the default address for each type.

public Address DefaultBillingAddress
{
    get
    {
        return BillingAddresses.First(x => x.IsDefault == true);
    }
}

我遇到的问题是如何告诉每个 ICollection 上的 AddressType 枚举之间的区别?

The problem I am having is how do I tell the difference between the AddressType enum on each ICollection?

推荐答案

您正在以两种不同的方式捕获地址类型的概念。

You are capturing the concept of "address type" in two different ways.

您有 AddressType 地址上的枚举,您也有单独的

You have your AddressType enum on Address, and you also have separate

public virtual ICollection<Address> BillingAddresses { get; set; }
public virtual ICollection<Address> ShippingAddresses { get; set; }

使用此模型肯定可以在BillingAddresses中以AddressType.Shipping结尾,反之亦然。

It's certainly possible with this model to end up with something in BillingAddresses with AddressType.Shipping and vice versa.

我建议您将对象模型简化为

I would suggest simplifying your object model to

public virtual ICollection<Address> Addresses { get; set; }

并在 Customer 上提供帮助方法

话虽如此,我不确定您的意思

Having said that, I'm not sure what you mean by


我遇到的问题是如何分辨每个ICollection上的AddressType枚举之间的区别?

The problem I am having is how do I tell the difference between the AddressType enum on each ICollection?

每个 ICollection 应该引用相同的 AddressType 枚举。

Each ICollection should reference the same AddressType enum.

使用我建议的方法,您可以编写

Using my suggested approach, you could write

public Address DefaultBillingAddress
{
    get
    {
        return Addresses
         .First(x => x.IsDefault == true && x.AddressType == AddressType.Billing);
    }
}

这篇关于实体框架5.0关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆