gpt4 book ai didi

sql - 创建没有外键的 LINQ 关联

转载 作者:行者123 更新时间:2023-12-04 22:41:21 26 4
gpt4 key购买 nike

是否可以在 LINQ 中使用类似 ContactAddress.Contact 的内容,而无需在 SQL Server 中在这两者之间创建外键关系(通过 Contact.Id <-> ContactAddress.ContactId)?

谢谢 :)

最佳答案

是的,您可以定义目标数据库中不存在的外键。 LINQ to SQL 生成的连接最终会相同,我的意思是:

from category in db.Categories
from product in category.Products
select new
{
Category = category,
Product = product
}

将生成与此相同的 T-SQL:
from category in db.Categories
join product in db.Products on category.CategoryId equals product.CategoryId
select new
{
Category = category,
Product = product
}

这将是这样的:
SELECT
*
FROM
Category INNER JOIN Product ON Category.CategoryId = Product.CategoryId

这是使遗留模式更有用的有用技术。

关于sql - 创建没有外键的 LINQ 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/961294/

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