【问题标题】:EF6 and POCO ClarificationEF6 和 POCO 澄清
【发布时间】:2013-12-06 11:59:57
【问题描述】:

我正在尝试了解以下如何使用 EF6 和 POCO 工作。

我有以下课程:

public class User {
    public int Id { get; set; }
    public virtual ICollection<UserLibrary> libraries{ get; set; }
}

public class UserLibrary {
    public int Id { get; set; }
    public DateTime CreationDate { get; set; }
    public ICollection<AbstractLibrary> { get; set; }

    public int UserId { get; set; }
    public virtual User User { get; set; }
}

public abstract class AbstractLibrary
{
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }

    public List<LibraryType> LibraryType{ get; set; }

    public enum LibraryType{ }
}

public class LibraryB: AbstractLibrary
{
    public int x { get; set; }
    public new List<LibraryType> LibraryType{ get; set; }

    public enum LibraryType
    {
        LibraryTypeAA,
        LibraryTypeAB,
        LibraryTypeAC
    }
}

public class LibraryB: AbstractLibrary
{
    public String y { get; set; }
    public new List<LibraryType> LibraryType{ get; set; }

    public enum LibraryType
    {
        LibraryTypeBA,
        LibraryTypeBB,
        LibraryTypeBC
    }
}

我有许多扩展 AbstractLibrary 的具体类,每个类都有不同的 LibraryType 枚举。

原因是一个 User 可以有多个 UserLibrary,而一个 UserLibrary 可以有多个具体的 LibraryA、LibraryB(每个都有不同的属性,但都有不同枚举的 LibraryType)。

我的问题是:这如何转化为 EF 中的表格,我将如何继续列出所有“LibraryTypes”?

我希望我的问题足够清楚。

【问题讨论】:

  • 为什么基础LibraryType 存在?好像没什么用。
  • AbstractLibrary 的实现有多少?您需要多灵活地支持更多?
  • 目前有 3 个,但我想让它这样,如果我想添加更多,它会非常简单。可以这样想,有 A 类、B 类、C 类,它们都有自己特定的枚举(库类型)。最重要的是,每个都可能有也可能没有用户要求的额外属性。
  • LibraryType 存在的原因是我不想为每种类型创建一个具体的类。我可以做的是有一个名为 LibraryTypeAA 的类,它扩展了 LibraryA,而后者又扩展了 AbstractLibrary。然而,这会导致一场噩梦。
  • 不过,您确实为每种类型创建了一个具体的枚举。枚举LibraryA.LibraryType 和属性LibraryA.LibraryTypeAbstractLibrary 的并行枚举和属性没有任何关系。从我所见,它们是完全独立的,类似的命名只会让人感到困惑。 ((AbstractLibrary)libraryA).LibraryType包含libraryA.LibraryType的值。

标签: c# entity-framework poco entity-framework-6


【解决方案1】:

这是 Sql 图。

这是使用数据库优先方法的代码:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class Library
    {
        public Library()
        {
            this.LibraryExtensionAs = new HashSet<LibraryExtensionA>();
            this.LibraryExtensionBs = new HashSet<LibraryExtensionB>();
            this.UserLibraries = new HashSet<UserLibrary>();
        }

        public int Id { get; set; }
        public string Description { get; set; }
        public Nullable<decimal> Price { get; set; }
        public Nullable<int> LinkId { get; set; }

        public virtual LibraryTypeLink LibraryTypeLink { get; set; }
        public virtual ICollection<LibraryExtensionA> LibraryExtensionAs { get; set; }
        public virtual ICollection<LibraryExtensionB> LibraryExtensionBs { get; set; }
        public virtual ICollection<UserLibrary> UserLibraries { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class LibraryExtensionA
    {
        public int Id { get; set; }
        public Nullable<int> LibraryId { get; set; }
        public Nullable<int> LinkId { get; set; }
        public Nullable<int> x { get; set; }

        public virtual Library Library { get; set; }
        public virtual LibraryTypeLink LibraryTypeLink { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class LibraryExtensionB
    {
        public int Id { get; set; }
        public Nullable<int> LibraryId { get; set; }
        public Nullable<int> LinkId { get; set; }
        public string y { get; set; }

        public virtual Library Library { get; set; }
        public virtual LibraryTypeLink LibraryTypeLink { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class LibraryType
    {
        public LibraryType()
        {
            this.LibraryTypeLinks = new HashSet<LibraryTypeLink>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<LibraryTypeLink> LibraryTypeLinks { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class LibraryTypeLink
    {
        public LibraryTypeLink()
        {
            this.Libraries = new HashSet<Library>();
            this.LibraryExtensionAs = new HashSet<LibraryExtensionA>();
            this.LibraryExtensionBs = new HashSet<LibraryExtensionB>();
        }

        public int Id { get; set; }
        public Nullable<int> TypeId { get; set; }

        public virtual ICollection<Library> Libraries { get; set; }
        public virtual ICollection<LibraryExtensionA> LibraryExtensionAs { get; set; }
        public virtual ICollection<LibraryExtensionB> LibraryExtensionBs { get; set; }
        public virtual LibraryType LibraryType { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public User()
        {
            this.UserLibraries = new HashSet<UserLibrary>();
        }

        public int Id { get; set; }

        public virtual ICollection<UserLibrary> UserLibraries { get; set; }
    }
}
namespace EF6
{
    using System;
    using System.Collections.Generic;

    public partial class UserLibrary
    {
        public int Id { get; set; }
        public Nullable<int> UserId { get; set; }
        public Nullable<int> LibraryId { get; set; }

        public virtual Library Library { get; set; }
        public virtual User User { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2013-06-05
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多