简体   繁体   English

EF6和POCO澄清

[英]EF6 and POCO Clarification

I am trying to get my head around how the following will work using EF6 and POCO. 我试图弄清楚如何使用EF6和POCO进行以下工作。

I have the following classes: 我有以下课程:

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
    }
}

I have a number of concrete classes that extend AbstractLibrary and each has a different LibraryType enum. 我有许多扩展AbstractLibrary的具体类,每个类都有不同的LibraryType枚举。

The reasoning is that a User can have many UserLibrary, and a UserLibrary can have many concrete LibraryA, LibraryB (each having different properties, but all have LibraryType with different enum). 原因是一个用户可以有很多UserLibrary,而一个UserLibrary可以有许多具体的LibraryA,LibraryB(每个都有不同的属性,但是所有的LibraryType都有不同的枚举)。

My question is: How does this translate to tables in EF, and how would I go on about listing all 'LibraryTypes' ? 我的问题是:这如何转换为EF中的表,我将如何继续列出所有“ LibraryTypes”?

I hope my question is clear enough. 我希望我的问题很清楚。

Here's the Sql Diagram. 这是Sql图。 在此处输入图片说明

Here's the code using Database First approach: 这是使用数据库优先方法的代码:

//------------------------------------------------------------------------------
// <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; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM