开发者

Compute value on insert in entity framework

OK here is my function (simplified a bit):

public int CreateTestRun(int testID)
{
    var testRun = new TestRun
    {
        TestID = testID,
        TestName =
            TE.Tests.Where(t => t.TestID == testID).Select(t => t.Name).First()
    };

    TE.TestRuns.AddObject(testRun);
    TE.SaveChanges();

    return testRun.TestRunID;
}

As you can see, we are denormalizing here a bit by pulling in the test name as well as the TestId. This is on purpose, so I don't want to remove that. This works fine, but it performs a database query to get the test name and then another to do the insert. What I want is a way to get the test name on the insert. I know I can do this with a trigger easily, but I was wondering if there is a way to do this 开发者_Python百科in Entity Framework.


There is no way to do this in entity framework without selecting the name first. If you want just insert which will set the name as well use either database trigger or create stored procedure for test run inserting and let the procedure select the name (this will be your choice if you want to get name in your application after insert). Import the procedure into your model and map it to insert operation on TestRun entity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜