在实体框架中每个层次结构结构的表中组合两个类

发布于 2025-02-10 15:08:45 字数 1046 浏览 5 评论 0原文

我们正在使用实体框架的每个层次结构继承技术使用表。 我将在此页面上以基本类的方式进行示例类: tabet模型。请不要将班级名称和属性作为集。我不想暴露任何关键的事情。这个问题与结构有关。

public class Contract
{
    public int ContractId { get; set; }
    public DateTime StartDate { get; set; }
    public int Months { get; set;}
    public decimal Charge { get; set; }
}

public class MobileContract : Contract
{
    public string MobileNumber { get; set; }
}

public class TvContract : Contract
{
    public PackageType PackageType { get; set; }
}

public class BroadBandContract : Contract
{
    public int DownloadSpeed { get; set; }
}

public enum PackageType
{
    S, M, L, XL
}

现在,在我们的情况下,我们的情况是,有一份合同可以是tvontract宽带合同,例如A MultiCntract。我们不知道该如何处理,因为我们不能从两个类中继承。 如果我们查询dbset< tvontract>dbset< broadbandantract>我们还希望获得Multicnarts s。

有人知道如何处理这种情况吗?

We're are using the table per hierarchy inheritance technique with Entity Framework.
I'll take the example classes from this page as the basic classes: Table per Hierarchy Inheritance - The Model. Please don't take the class names and properties as set. I didn't want to expose anything critical. This question is just about the structure.

public class Contract
{
    public int ContractId { get; set; }
    public DateTime StartDate { get; set; }
    public int Months { get; set;}
    public decimal Charge { get; set; }
}

public class MobileContract : Contract
{
    public string MobileNumber { get; set; }
}

public class TvContract : Contract
{
    public PackageType PackageType { get; set; }
}

public class BroadBandContract : Contract
{
    public int DownloadSpeed { get; set; }
}

public enum PackageType
{
    S, M, L, XL
}

Now, in our case we have the situation that there is a contract that can be both, a TvContract and a BroadBandContract, e.g. a MultiContract. We don't know how to deal with this since we cannot inherit from two classes.
If we query the DbSet<TvContract> or DbSet<BroadBandContract> from the we also want to get the MultiContracts.

Does someone have an idea how to deal with this scenario?

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

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

发布评论

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

评论(1

幸福%小乖 2025-02-17 15:08:45

我看到我们当前的阶级结构是继承的。在我看来,它看起来像是构图。合同可能包含不同的分包合同,一旦合同到期,分包也会过期。但是,分包合同具有不同的类型,这就是您可以使用的优势。您可能还会看到某人添加或删除其合同类型的软件包并不是真正静态的要求。我会设计来处理这一点。我还希望通过分包合同单独使用月份 允许使用分包合同 obejcts的可变启动日期和结束日期。

我将重新设计这样的类,然后仅与分包合同过滤器查询合同。

public class Contract
{
    public int ContractId
    {
        get;
        set;
    }
    public DateTime StartDate
    {
        get;
        set;
    }
    public int Months
    {
        get;
        set;
    }
    public decimal Charge
    {
        get;
        set;
    }

    public bool IsMultiContract
    {
        get
        {
            return SubContracts.GroupBy(x => x.ContractType).Count() > 0;
        }
    }

    /// <summary>
    /// Optional unique check for subcontract types may be added
    /// </summary>
    public List<SubContract> SubContracts
    {
        get;
        set;
    }
}

public enum ContractType
{
    Broadband,
    Mobile,
    TV
}


public class SubContract
{
    public ContractType ContractType
    {
        get;
        set;
    }
}

public class MobileContract : SubContract
{
    public string MobileNumber
    {
        get;
        set;
    }
}

public class TvContract : SubContract
{
    public PackageType PackageType
    {
        get;
        set;
    }
}

public class BroadBandContract : SubContract
{
    public int DownloadSpeed
    {
        get;
        set;
    }
}

public enum PackageType
{
    S,
    M,
    L,
    XL
}

I see our current class structure is of inheritance. While to me it looks like composition. A contract may contain different subcontracts and once the contract expires, subcontracts too expire. However, subcontracts are of different types and that is what you could use for your advantage. You may also see a requirement where someone would add or remove the packages they have thus contract type is not really static. I would design to handle that. I would have also preferred an end date or Months individually on SubContract allowing for variable start and end dates for SubContract obejcts.

I would redesign the classes like this and then only query Contract potentially with SubContract based filters.

public class Contract
{
    public int ContractId
    {
        get;
        set;
    }
    public DateTime StartDate
    {
        get;
        set;
    }
    public int Months
    {
        get;
        set;
    }
    public decimal Charge
    {
        get;
        set;
    }

    public bool IsMultiContract
    {
        get
        {
            return SubContracts.GroupBy(x => x.ContractType).Count() > 0;
        }
    }

    /// <summary>
    /// Optional unique check for subcontract types may be added
    /// </summary>
    public List<SubContract> SubContracts
    {
        get;
        set;
    }
}

public enum ContractType
{
    Broadband,
    Mobile,
    TV
}


public class SubContract
{
    public ContractType ContractType
    {
        get;
        set;
    }
}

public class MobileContract : SubContract
{
    public string MobileNumber
    {
        get;
        set;
    }
}

public class TvContract : SubContract
{
    public PackageType PackageType
    {
        get;
        set;
    }
}

public class BroadBandContract : SubContract
{
    public int DownloadSpeed
    {
        get;
        set;
    }
}

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