0

有没有办法使用实体和 LinQ 编写相同的 SQL 原子指令?

IF EXISTS (SELECT * FROM MyTable WHERE ID = @Id) UPDATE MyTable SET name = @name ELSE INSERT INTO MyTable (@Id, @name)

还是您需要从 EF 中调用存储过程?

4

1 回答 1

0

The Entity Framework keeps track of the lifespan of objects:

  • If the object was initialized from a query, the framework understands a record should exist in the database and will perform an UPDATE when pushing changes back to the database.
  • If the object was initialized in code, the framework understands it as a "new" object and performs an INSERT when pushing the changes to the database.

If you wish to have a single piece of SQL called, regardless of whether an INSERT or UPDATE operation is required, you will have to specify a stored procedure.

于 2010-04-19T10:25:10.187 回答