简体   繁体   中英

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.

If I could get an Entity Framework Query to store both id's in customer_hobbies table it would help me abundantly.

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.

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 .

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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