How can I separate "Data" from my console application?
I have two projects:
- Console application
- Class Library 开发者_高级运维
I want the Class library
to define the classes, create edmx files and to have a partial connection string, like:
<add name="BlogEntities" connectionString="metadata=res://*/Blog.csdl|res://*/Blog.ssdl|res://*/Blog.msl;provider=System.Data.SqlClient;provider connection string='{0}'" providerName="System.Data.EntityClient" />
I want my Class Library to define the csdl
, ssdl
and msl
files. My Console application doesn't care about the metadata, it will only define the database
, user
and password
.
How can I split the connection string in two like this?
from my memory, if you create the model in a class lib, VS will create an app.config in the lib project.
Simply merge the content of this file in the app.config of the console library.
in this scenario, you can have a "design" config in the app.config file of the lib, and a run time config in the app.config of the console application.
Create a class in your library (e.g. Connector
) that allows you to provide whatever connection values you will need (database, user, password). Then use this class in your console application and provide the necessary values however you see fit.
I found out that I can name all entities with the same name, e.g. Entities
. My app.config
will need to be on the Console Application, but I can use a single ConnectionString
.
<add name="Entities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLExpress;Initial Catalog=Test;Persist Security Info=True;User ID=test;Password=test;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The metadata can be set to res://*/
and it will work globally. From MSDN:
Model and mapping metadata used by the Entity Framework is loaded into a MetadataWorkspace. This metadata is cached globally and is available to other instances of ObjectContext in the same application domain.
I can also force that any plug-able module will use a ConnectionString
passing on the context constructor:
new Blogs.Data.Entities("name=Entities");
精彩评论