Pros and Cons of putting a db context in static class library

后端 未结 2 946
攒了一身酷
攒了一身酷 2021-01-02 14:00

We have a static class library to deal with repeated multi-contextual tasks. Is it bad practice to create an EF db context as member of a static class?

DB contexts a

2条回答
  •  -上瘾入骨i
    2021-01-02 14:37

    It depends on many things, but here are some thoughts e.g.:

    • If you're using EF on service layer - then the concurrency might be an issue as I don't think that using EF context is thread safe, ie that you can use it from all threads at the same time without problems
    • If you have your entities tracked by the context (and I think even if you don't), the context would in time get quite large, eventually it could contain all of your database entities and then you'll run into performance problems

    Either way, I think it's a bad idea.

提交回复
热议问题