Moving Entity Framework model into class library from web project

前端 未结 2 787
梦谈多话
梦谈多话 2020-12-09 04:19

I am using Entity Framework and recently came to realize the benefits of having your EF model in another project within the same solution so that I can build multiple UIs fr

相关标签:
2条回答
  • 2020-12-09 04:42

    The UI project requiring a connection string is ridiculous. Only the class library that contains the Entity Framework specifics should require either a connection string to the database or even a reference to Entity Framework. This is leaking abstractions into your UI project, which should not even know what a database is.

    0 讨论(0)
  • 2020-12-09 04:47

    This is by design; while the config file in the class library is what the designer will use, the configuration file of the actual application is what will get used at runtime. Whether that's Web.config for an ASP.NET project or App.config for a Winforms or WPF project, it's the application configuration file (or something higher up, like Machine.config) that will be used; the file in the class library is not part of the application.

    If you're trying to provide an EF model that will work without having to specify the connection string in the application or web configuration file, then you'll have to store the connection string some other way (you could always hard-code it) and pass it into the appropriate overload of your context's constructor.

    My solution is generally to provide a static parameterless function on the context itself that calls this overload with the appropriate connection string.

    0 讨论(0)
提交回复
热议问题