LinqToSql declare and instantiate DataContext best practice?

后端 未结 5 1830
-上瘾入骨i
-上瘾入骨i 2020-12-01 14:32

What\'s the best practice in terms of setting up my DataContext for easy access in my extended LinqToSql classes?

For example, I have a \"User\" entity in my dbml a

5条回答
  •  我在风中等你
    2020-12-01 15:31

    First, make sure you are disposing your DataContext when you're done! He can be a heavy little bastard (edit not heavy to instantiate, but heavy to keep around if you keep using it without disposing); you don't want old DataContexts hanging around in memory.

    Second, the DataContext is intended to represent a single logical transaction. E.g. you should create a new one every time you want to start a new transaction, and get rid of it when that transaction is complete. So for your purposes, that is probably the scope of the GetUser method. If you have a series of DB calls that need to be made as a group, they should all use the same DC before getting rid of it.

提交回复
热议问题