gpt4 book ai didi

entity-framework-core - 如何防止在 Entity Framework 核心 3.1 中对拥有的属性进行左连接?

转载 作者:行者123 更新时间:2023-12-04 17:30:40 30 4
gpt4 key购买 nike

public class Employee
{
public string FullName { get; set; }
public string Code { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
public DateTime DateOfBirth { get; set; }
public string Status { get; set; }

// # Address
public Address Address { get; set; }
}

public class Address
{
public string Address1 { get; set; }
public string? Address2 { get; set; }
public string City { get; set; }
public string? Region { get; set; }
public string PostCode { get; set; }
public Country Country { get; set; }

private Address() {}
}

实体配置
public class EmployeeConfiguration : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.OwnsOne(e => e.Address);
}
}

生成的 SQL
SELECT t."Id", t."BsonId", t."Code", t."CompanyId", t."Created", t."DateOfBirth", t."FullName", t."Gender", t."LastModified", t."MaritalStatus", t."Status", t1."Id", t1."Address_Address1", t1."Address_Address2", t1."Address_City", t1."Address_PostCode", t1."Address_Region"
FROM (
SELECT e."Id", e."BsonId", e."Code", e."CompanyId", e."Created", e."DateOfBirth", e."FullName", e."Gender", e."LastModified", e."MaritalStatus", e."Status"
FROM "I180814-04-edm"."Employees" AS e
ORDER BY (SELECT 1)
LIMIT @__p_1 OFFSET @__p_0
) AS t
LEFT JOIN (
SELECT e0."Id", e0."Address_Address1", e0."Address_Address2", e0."Address_City", e0."Address_PostCode", e0."Address_Region", e1."Id" AS "Id0"
FROM "I180814-04-edm"."Employees" AS e0
INNER JOIN "I180814-04-edm"."Employees" AS e1 ON e0."Id" = e1."Id"
WHERE (e0."Address_Region" IS NOT NULL) OR ((e0."Address_PostCode" IS NOT NULL) OR ((e0."Address_City" IS NOT NULL) OR ((e0."Address_Address2" IS NOT NULL) OR (e0."Address_Address1" IS NOT NULL))))
) AS t0 ON t."Id" = t0."Id"
LEFT JOIN (
SELECT e2."Id", e2."Address_Address1", e2."Address_Address2", e2."Address_City", e2."Address_PostCode", e2."Address_Region", e3."Id" AS "Id0"
FROM "I180814-04-edm"."Employees" AS e2
INNER JOIN "I180814-04-edm"."Employees" AS e3 ON e2."Id" = e3."Id"
WHERE (e2."Address_Region" IS NOT NULL) OR ((e2."Address_PostCode" IS NOT NULL) OR ((e2."Address_City" IS NOT NULL) OR ((e2."Address_Address2" IS NOT NULL) OR (e2."Address_Address1" IS NOT NULL))))
) AS t1 ON t."Id" = t1."Id"

我错过了什么?我到处搜索,但无论如何都找不到修改 Ef core 中为 OwneredProperties 生成的 SQL。有没有办法设置关系,以便 ef core 将属性作为单个模型进行投影,而不是将其检测为关系并生成左连接。

最佳答案

实际上,生成的 SQL 语句(左连接)没有任何问题。
您可以使用正确的谓词来确保 address不像内连接那样空,没有任何性能影响:

var result = await dbcontext.Employee.Include(x => x.Include(y => y.Address)).Where(p => p.Address != null).FirstOrDefaultAsync();
这是一种非常常见的方法。

关于entity-framework-core - 如何防止在 Entity Framework 核心 3.1 中对拥有的属性进行左连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60039720/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com