简体   繁体   English

需要帮助将此 SQL 查询转换为实体框架查询

[英]Needed help in converting this SQL query to Entity Framework Query

INSERT INTO customer_hobbies(cust_id,hobby_id)
VALUES(913,4)
SELECT c.cust_id FROM customers c
JOIN customer_hobbies ch
ON ch.cust_id = c.cust_id;

Now, both cust_id,hobby_id are foreign keys in customer_hobbies table and cust_id is a primary key in customers table.现在, cust_id,hobby_id都是customer_hobbies表中的外键,cust_id 是customer_hobbies表中的主键。

If I could get an Entity Framework Query to store both id's in customer_hobbies table it would help me abundantly.如果我能得到一个实体框架查询来将两个 id 存储在customer_hobbies表中,那将对我有很大帮助。

Thank You.谢谢你。

For the first query, you can use:对于第一个查询,您可以使用:

// INSERT INTO customer_hobbies(cust_id,hobby_id)
// VALUES(913,4)
db.CustomerHobbies.AddObject(new CustomerHobbies { CustID = 913, HobbyID = 4 });
db.SaveChanges();

(the exact property names depend on the entity framework conversion configuration) (确切的属性名称取决于实体框架转换配置)

For the second query, since you're not using any field from customer_hobbies, you can just query customers.对于第二个查询,由于您没有使用 customer_hobbies 中的任何字段,您可以只查询客户。

Even if you want to get the column c.cust_id , since that is also the join key, you can just query it from customer_hobbies .即使您想获取列c.cust_id ,因为这也是连接键,您也可以从customer_hobbies查询它。

For example:例如:

var ch = db.CustomerHobbies.Where(/* some predicate */).First();
var customerID = ch.CustID;

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

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