簡體   English   中英

使用 EF Core 如何進行簡單的連接以獲得一個值

[英]Using EF Core how to do a simple join to get one value

我正在使用 EF Core 3.1.6。 我有兩個 model 類:

[Table("Stuff")]
public class Stuff
{
    [Key]
    public int StuffId {get;set;}
    public string NameOfStuff {get;set;}
    public int OtherStuffId {get;set;}
    ///this is what I want from the other model
    public string OtherStuffName {get; set;}

    public OtherStuff OtherStuff {get;set;}
}

[Table("OtherStuff")]
public class OtherStuff
{
    [Key]
    public int OtherStuffId {get;set;}
    public string OtherStuffName {get;set;}
  
    public ICollection<Stuff> Stuff {get;set;}
}

在我的dbcontext ,我有:

public DbSet<Stuff> Stuffs {get;set;}
public DbSet<OtherStuff> OtherStuffs {get;set;}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // I'm not really sure what to do here???
    // I've tried this 
    modelBuilder.Entity<Stuff>()
            .HasOne<OtherStuff>(s => s.OtherStuff)
            .WithMany(g => g.Stuff)
            .HasForeignKey(s => s.OtherStuffId);
}

這將為“OtherStuff”返回一個空數據集——這不是我想要的。 有什么幫助嗎?

這是如何調用它的示例:

var stuff = _context.Stuff.OtherStuffName;  //this is null
var stuffList = _context.Stuff.OtherStuff; //this is null

構建查詢時是否使用 .Include(x => x.OtherStuff) ? 如果使用.Include 不包含在查詢中,則不會加載關系屬性。

要獲取具有關系的實體,您應該像這樣使用Include

var stuff = _context.Stuff.Include(x=>x.OtherStuff).First();
var otherStuffName=stuff.OtherStuff.OtherStuffName;

無需在Stuff實體中添加屬性OtherStuffName ,因為它位於 OtherStuff 實體中。

至於你的主要問題 -

使用 EF Core 如何進行簡單的連接以獲得一個值

我真的建議您首先通過以下文檔 go 清楚地了解如何使用Entity Framework Core執行查詢 -

至於您提供的兩個示例-

  1. 我不知道您是如何設法與他們一起生產null的。 這兩行都不應該編譯,因為您的DbContext中沒有名為Stuff的屬性。 即使那些是錯字,而您實際上是要寫-
var stuff = _context.Stuffs.OtherStuffName;
var stuffList = _context.Stuffs.OtherStuff;

他們仍然無法編譯,因為DbSet<Stuff>沒有屬性OtherStuffNameOtherStuff

  1. 如果您嘗試獲取OtherStuffName ,請執行以下任一操作 -
// returns "OtherStuffName" of an "OtherStuff" whose "OtherStuffId" is 3
var name = _context.OtherStuffs
            .Where(p=> p.OtherStuffId == 3)
            .Select(p=> p.OtherStuffName)
            .FirstOrDefault();

// OR,
// returns "OtherStuffName" of the "OtherStuff" which is related to a "Stuff" whose "StuffId" is 3
var name = _context.Stuffs
            .Where(p=> p.StuffId == 3)
            .Select(p=> p.OtherStuff.OtherStuffName)
            .FirstOrDefault();
  1. 如果您正在嘗試獲取Stuff及其相關OtherStuff的列表,請執行 -
var stuffList = _context.Stuffs
                .Include(p=> p.OtherStuff)
                .ToList();

其他一些考慮:

  1. 您的 model 定義沒問題,除了OtherStuff model 中的屬性Stuff應該命名為Stuffs (復數形式),因為它是Stuff實體的集合。 一致的命名很重要。
  2. 您的 model 配置沒問題,但.HasOne()並不真正需要通用類型參數 - .Hasone(s => s.OtherStuff)可以正常工作。

您可以將 Stuff 集合設為虛擬 - 這將啟用延遲加載,並且 EF 會在運行中自動為您加載 Stuff。 希望它對你有用:)

[Table("OtherStuff")]
public class OtherStuff
{
  public OtherStuff()
  {
      Stuff = new List<Stuff>();
  }

  ...

  public virtual ICollection<Stuff> Stuff {get;set;}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM