简体   繁体   English

ASP.NET Core DB First脚手架连接不起作用

[英]ASP.NET Core DB First scaffolding Join doesn't work

I've created a test ASP.NET Core project. 我已经创建了一个测试ASP.NET Core项目。 I've generated the models from an existing DB using the Scaffold-DbContext command. 我已经使用Scaffold-DbContext命令从现有数据库中生成了模型。

Everything went fine. 一切都很好。 I've added an ApiController to return the data, and if I query, using LINQ, simple flat table data or I make joins on tables where the foreign keys have been explicitly defined in the DB it works. 我添加了一个ApiController来返回数据,并且如果我使用LINQ,简单的平面表数据进行查询,或者我对已在数据库中显式定义了外键的表进行联接,那么它将起作用。

But if I make a query joining two tables where the foreign keys have not been set it doesn't return anything . 但是如果我进行查询联接了两个没有设置外键的,它不会返回任何信息

Please note that the two tables are related to each other by an integer ID ( MktId ). 请注意,两个表通过整数ID( MktId )相互关联。

Entity models generated by the scaffold: 支架生成的实体模型:

public partial class MonthlyPrice
{
    public int MpId { get; set; }        
    public int MktId { get; set; }
    public int CmId { get; set; }
    public decimal MpPrice { get; set; }

    public Commodities Cm { get; set; }
    public Currencies Cur { get; set; }
    public PriceTypes Pt { get; set; }
    public UnitOfMeasure Um { get; set; }
}

public partial class Commodities
{
    public Commodities()
    {
        MonthlyPriceItem = new HashSet<MonthlyPriceItem>();
    }

    public int CmId { get; set; }
    public string CmName { get; set; }
    public int CmCatId { get; set; }

    public ICollection<MonthlyPrice> MonthlyPriceItem { get; set; }
}

public partial class Markets
{
    public int MktId { get; set; }
    public string MktName { get; set; }
}

the following query return results: 以下查询返回结果:

var price= (from m in db.MonthlyPrice
           join c in db.Commodities on m.CmId equals c.CmId
           select new
           {
               c.CmName,
               m.MpPrice
           });

but this one doesn't return anything: 但这不返回任何内容:

var price= (from m in db.MonthlyPrice
           join mk in db.Markets on m.MktId equals mk.MktId
           select new
           {
               m.MpPrice,
               mk.MktName
           });

Please note that both queries on Entity Framework 6.x on ASP.NET 4.7 works perfectly . 请注意, 在ASP.NET 4.7上的Entity Framework 6.x上的两个查询都可以正常运行

Should I have to specify all the foreign key in the DB to make EFCore works correctly? 我是否必须在数据库中指定所有外键才能使EFCore正常工作?

(Db is not always designed by me!!) (DB并非总是由我设计!!)

UPDATE 更新

The model builder for the Markets has been generated by scaffold-dbcontext command like the following: 市场的模型构建器是通过scaffold-dbcontext命令生成的 ,如下所示:

modelBuilder.Entity<Markets1>(entity =>
{
    entity.HasKey(e => e.MktId);

    entity.ToTable("__Markets");

    entity.Property(e => e.MktId).HasColumnName("mkt_id");

    entity.Property(e => e.MktName)
        .IsRequired()
        .HasColumnName("mkt_name")
        .HasMaxLength(250);    
});

Respect to the one generated for Commodities table I've noticed that there is the line entity.ToTable("__Markets"); 关于为Commodities表生成的那个,我注意到有一行object.ToTable(“ __ Markets”); that looks very strange to me. 这对我来说很奇怪。

  1. If you want to fight with it a bit: 如果您想与之抗争:

Update to at least .NET Core 2.1 至少更新到.NET Core 2.1

Preserve the original database names to remove the funk in the migration. 保留原始数据库名称以删除迁移中的功能。 Use the -UseDatabaseNames option. 使用-UseDatabaseNames选项。

scaffold-dbcontext  -UseDatabaseNames 
  1. Else, continue: 否则,继续:

Annotations might be valuable either way if your ids or table names on the current database are funky (spaces, prefixes, etc...) 如果您的ID或当前数据库上的表名很时髦(空间,前缀等),则注释可能很有价值。

Add Markets to MonthlyPrice Class as a foreign key. 将市场添加到MonthlyPrice类作为外键。 Make the ids obvious to the migration using data annotations. 使用数据注释使ID对迁移显而易见。

[Table(“MonthlyPrice”)]
public partial class MonthlyPrice
{
    [Key]
    public int MpId { get; set; }        

    [ForeignKey("Markets")]
    public int MktId { get; set; }
    public Markets Mkt { get; set; }

    [ForeignKey("Commodities")]
    public int CmId { get; set; }
    public Commodities Cm { get; set; }

    public decimal MpPrice { get; set; }
    public Currencies Cur { get; set; }
    public PriceTypes Pt { get; set; }
    public UnitOfMeasure Um { get; set; }
}
[Table(“Commodities”)]
public partial class Commodities
{
    public Commodities()
    {
        MonthlyPriceItem = new HashSet<MonthlyPriceItem>();
    }

    [Key]
    public int CmId { get; set; }
    public string CmName { get; set; }
    public int CmCatId { get; set; }

    public ICollection<MonthlyPrice> MonthlyPriceItem { get; set; }
}

[Table(“Markets”)]
public partial class Markets
{
    [Key]
    public int MktId { get; set; }
    public string MktName { get; set; }
}

If recreating database: 如果重新创建数据库:

Add-Migration awesome

Update-Database awesome

Else, if just adding a migration: 否则,如果仅添加迁移:

Add-Migration awesome –IgnoreChanges

I guess the great thing is, you have a golden opportunity to make it better than the last guy. 我想最棒的是,您有了千载难逢的机会使它比上一个家伙更好。 Starting out with Same-Same on everything (table names, column names, keys, would be nice). 从所有内容(表名,列名,键等)开始都是相同的。 Clean up all the differences. 清理所有差异。

There are clearly differences in both your table name and primary key names currently. 当前,表名和主键名显然存在差异。

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

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