Change Entity framework database schema at runtime

前端 未结 8 2641
花落未央
花落未央 2021-02-20 13:02

In most asp.net applications you can change the database store by modifing the connectionstring at runtime. i.e I can change from using a test database to a production database

8条回答
  •  难免孤独
    2021-02-20 13:39

    When I create a new "ADO.NET Entity Data Model", there are two properties "Entity Container Name" and "Namespace" available for editing in design view.. Using the namespace.EntityContainerName, you can create a new instance specifying a connection string.

    MyEntities e = new MyEntities("connstr");
    e.MyTable.Count();

    I'm not sure if this helps you or not, good luck!

    Also, this is a good case for multiple layers (doesn't have to be projects, but could be).

    Solution
    * DataAccess - Entities here
    * Service - Wraps access to DataAccess
    * Consumer - Calls Service

    In this scenario, the consumer calls service, passing in whatever factor determines which connection string is used. The service then instantiates an instance of data access passing in the appropriate connection string and executes the consumer's query.

提交回复
热议问题