How to publish ado.net Entity Framework EDM

Source: Internet
Author: User
Tags connectionstrings

Preface

In the development ofProgramUsually, EDM (Entity Data Model) is put in a class library separately.

Pay attention to some issues when releasing the class library. Otherwise, some problems may occur.

Start

The solution contains two projects: efdemo. console and efdemo. models. Here I put EDM in efdemo. Models class library.


Add an "ADO. NET Entity Data Model" item in the efdemo. Models class library named "usermodel ". After you configure the connection through the wizard, An EDM is created. EDM includes three files (. CSDL,. MSL, And. SSDL files ).

After EDM is created, some information will be added to the <connectionstrings> node in the configuration file (App. config here. In this example, after usermodel. edmx is created, a subnode is added to the configuration file:

<Connectionstrings>
<Add name = "userentities" connectionstring ="Metadata =. \ usermodel. CSDL |. \ usermodel. SSDL |. \ usermodel. MSL; Provider = system. data. sqlclient; provider connection string = & quot; Data Source = xxxxxx; initial catalog = saasdb; persist Security info = true; user id = sa; Password = xxx; multipleactiveresultsets = false & quot; "providername =" system. data. entityclient "/>
</Connectionstrings>

Note that the highlighted "metadata =. \ usermodel. CSDL |. \ usermodel. SSDL |. \ usermodel. MSL ", specify the location of the three files here ,". \ "indicates that the three files will be deployed to the" build output path "position after the build.

Return to the example, efdemo. Console project references efdemo. Models project, so that you can use usermodel in efdemo. Console project.CodeCopy the above configuration file information to the app. config of the efdemo. Console project and run Ctrl + F5. the following error message is displayed:

System. data. metadataexception: the specified metadata path is not valid. A valid path must be either an existing Directory, an existing file with extension '. CSDL ','. SSDL ', or '. msl', or a URI that identifies an embedded resource.

Obviously, the three files usermodel. CSDL, usermodel. SSDL, and usermodel. MSL are not found. They are not found in the build output path of efdemo. console.

One solution is to copy the three files to the bin of the efdemo. Console project after each efdemo. Models project build, but this is obviously not the best practice.

You can use the following configurations to better solve this problem.

1. Open the attributes Panel of the usermodel and set the value of the "metadata artifact processing" option to "embed in output assembly ". Indicates that these three files will be embedded into the assembly.

2. Open the property panel of usermodel. edmx and confirm that the build action value is "entitydeploy ".

After the preceding configuration is complete, the userentities node value in the configuration file is automatically changed:

<Connectionstrings>
<Add name = "userentities" connectionstring ="Metadata = Res: // */usermodel. CSDL | res: // */usermodel. SSDL | res: // */usermodel. MSL; Provider = system. data. sqlclient; provider connection string = & quot; Data Source = xxxxxx; initial catalog = saasdb; persist Security info = true; user id = sa; Password = xxx; multipleactiveresultsets = false & quot; "providername =" system. data. entityclient "/>
</Connectionstrings>

Pay attention to the highlighted "metadata = Res: // */usermodel. CSDL | res: // */usermodel. SSDL | res: // */usermodel. MSL ". "*" Indicates that it will go to all aseembly to find the three files embedded in assembly. Of course, if we clearly know that these three files are placed in the efdemo. Models. dll Assembly, we should specify to find them only in the Assembly, instead of wasting time searching in all the assemblies:

<Connectionstrings>
<Add name = "userentities" connectionstring ="Metadata = Res: // efdemo. models/usermodel. CSDL | res: // efdemo. models/usermodel. SSDL | res: // efdemo. models/usermodel. MSL; Provider = system. data. sqlclient; provider connection string = & quot; Data Source = xxxxxx; initial catalog = saasdb; persist Security info = true; user id = sa; Password = xxx; multipleactiveresultsets = false & quot; "providername =" system. data. entityclient "/>
</Connectionstrings>

Update the configuration information to the app. config of the efdemo. Console project, and press Ctrl + F5 to run normally.

By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.