EF6和POCO澄清

时间:2013-11-21 20:00:30

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

我试图了解以下如何使用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枚举。

原因是用户可以拥有许多UserLibrary,UserLibrary可以有许多具体的LibraryA,LibraryB(每个都有不同的属性,但都具有不同枚举的LibraryType)。

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

我希望我的问题很清楚。

1 个答案:

答案 0 :(得分:1)

这是Sql Diagram。 enter image description here

以下是使用Database First方法的代码:

//------------------------------------------------------------------------------
// <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; }
    }
}
相关问题