Entity Framework 核心 2.1 关系问题

Entity Framework Core 2.1 trouble with relationships

我正在尝试将以下 sql 查询转换为 entity framework,但 运行 出现问题,出现的列未加入 tables。

SELECT 
a.TABLE_NAME AS tableName,
b.COLUMN_NAME AS columnName,
b.DATA_TYPE AS dataType,
CASE WHEN b.IS_NULLABLE = 'NO' THEN 'FALSE' ELSE 'TRUE' END AS allowNull
FROM INFORMATION_SCHEMA.TABLES a
INNER JOIN INFORMATION_SCHEMA.COLUMNS b ON a.TABLE_NAME = b.TABLE_NAME

这是我目前所拥有的

数据库上下文:

using Microsoft.EntityFrameworkCore;

namespace EFCoreTest.Models 
{
    public class InformationContext : DbContext
    {   
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        {
            optionsBuilder.UseSqlServer(@"Server=localhost;Database=master;Trusted_Connection=True;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Table>()
                .HasKey(t => new {t.tableName, t.catalogName, t.schemaName});

            modelBuilder.Entity<Column>()
                .HasOne(c => c.table)
                .WithMany(c => c.columns)
                .HasForeignKey(c => new {c.tableName, c.catalogName, c.schemaName});

        }

        public DbSet<Table> Tables {get; set;}
        public DbSet<Column> Columns {get; set;}
    }
}

第 Class 列:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace EFCoreTest.Models
{
    [Table("COLUMNS", Schema = "INFORMATION_SCHEMA")]
    public class Column
    {
        [Key]
        [Column("COLUMN_NAME")]
        public String columnName {get; set;}
        [Column("DATA_TYPE")]
        public String dataType {get; set;}
        [Column("IS_NULLABLE")]
        public String allowNUlls {get; set;}
        [ForeignKey("Table")]
        [Column("TABLE_NAME")]
        public String tableName {get; set;}
        [ForeignKey("Table")]
        [Column("TABLE_CATALOG")]
        public String catalogName {get; set;}
        [ForeignKey("Table")]
        [Column("TABLE_SCHEMA")]
        public String schemaName {get; set;}
        public Table table {get; set;}

    }
}

Table class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace EFCoreTest.Models
{
    [Table("TABLES" , Schema = "INFORMATION_SCHEMA")]
    public class Table
    {
        [Key]
        [Column("TABLE_NAME")]
        public String tableName {get; set;}
        [Key]
        [Column("TABLE_CATALOG")]
        public String catalogName {get; set;}
        [Key]
        [Column("TABLE_SCHEMA")]
        public String schemaName {get; set;}
        public ICollection<Column> columns {get; set;}

        protected Table() {columns = new List<Column>();}
    }
}

主要:

using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using EFCoreTest.Models;

namespace EFCoreTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using(InformationContext context = new InformationContext())
            {
                var results = context.Tables.Include(t => t.columns).ToList();

                foreach(var t in results)
                {
                    Console.WriteLine(t.tableName);
                    Console.WriteLine("-----------------------------");
                    var columns = t.columns.ToList();

                    foreach(var c in columns)
                    {
                        Console.WriteLine(c.columnName);
                    }

                    Console.WriteLine("");
                }
            }
        }
    }
}

代码运行良好,但在检查 table 实例时,所有列实例均为空。我感觉它与 table 和列之间的关系有关,但是在查看 efcore2.1 的关系文档后,我无法弄清楚我做错了什么。

如有任何帮助,我们将不胜感激。

更新: 使用附加键和相关数据加载更新了代码。

试试这个:

context.Tables.Include(t => t.columns).ToList();

首先欢迎来到stack overflow。

根据 Gonzalo 的回答,Include 语句可以让您包含一个集合:

context.Tables.Include(t => t.columns).ToList();

不过,我想强调一些您可以做的其他小改进,使您的代码随着时间的推移变得更加健壮和可维护。

  1. 请记住使用受保护的构造函数初始化您的实体以避免空指针异常,因为在大多数情况下返回带有实体的空集合应该是业务应用程序的有效场景:

    受保护Table() { 列=新列表(); }

  2. 使用 ICollection 而不是 List 来定义集合。

  3. C# 中的通用命名标准是在声明 public 属性和集合时使用 Pascal 大小写。

  4. 您正在混合定义关系的 2 种不同方式。

这个:

modelBuilder.Entity<Column>()
    .HasOne(c => c.table)
    .WithMany(c => c.columns)
    .HasForeignKey(c => c.tableForeignKey);

和您在 [Key] 等实体的相关属性上使用的注释实际上是做同一件事的两种不同方式。使用一个,最好先使用代码,即通过配置。

5,我建议使用单独的实体类型配置文件,否则你的模式最终将很难维护,示例基础配置:

public class BaseEntityConfiguration<TEntity> : IEntityTypeConfiguration<TEntity>
    where TEntity : BaseEntity
{
    public virtual void Configure(EntityTypeBuilder<TEntity> builder)
    {
        builder.HasKey(be => be.Guid);

        builder.Property(be => be.CreatedBy).IsRequired();

        builder.Property(be => be.CreatedDate).IsRequired();
    }
}

public class AddressConfiguration : BaseEntityConfiguration<Address>
{
    public override void Configure(EntityTypeBuilder<Address> builder)
    {
        builder.HasOne(a => a.Contact)
            .WithMany(c => c.Addresses)
            .HasForeignKey(a => a.ContactGuid);

        builder.HasOne(a => a.Partner)
            .WithMany(a => a.Addresses)
            .HasForeignKey(a => a.PartnerGuid);

        base.Configure(builder);
    }
}

在上下文中:

modelBuilder.ApplyConfiguration(new AddressConfiguration());

正如您可能注意到的那样,我还使用 BaseEntity 来保存所有重复属性,例如 Id,并从中派生出我的所有实体。我建议你也这样做。

希望对您有所帮助。