问题
When I try and use EntityFramework and MySQL on a Linux or Windows environment I run into the following problem:
Project 1: Contains EntityFramework edmx and logic to insert update data using the dbcontext class Project 2: References Project 1.
When I build the solution using msbuild the EntityFramework metadata files are embeded in Project1.dll
When I do a clean build with xbuild in a Linux environment or in a Windows environment the EntityFramework metadata files are missing
When you run the application you get the following error:
Unable to load the specified metadata resource.
I am using mono version 4.2.2
Does anybody know a solution for embedding the EntityFramework metadata files when using xbuild?
回答1:
I have implemented the following workaround until mono embeds the Entity Framework metadata artifacts
- Step1 - Update your EntityFramework Model's Metadata Artifact Processing property from "Embed in Output Assembly" to "Copy to Output Directory"
This copies the metadata artifact files to the bin folder of the project containing the .edmx(Project1)
Step2 - Add the following post build events to the referencing project(Project2) to copy the metadata artifact files to its bin. You can add them to the end of the .csproj project file. Replace Project1 with the name of your project.
<PropertyGroup> <PostBuildEvent Condition=" '$(OS)' != 'Unix' ">copy /Y "$(ProjectDir)..\Project1\bin\Debug\Models\*" "$(ProjectDir)\bin\Debug\" </PostBuildEvent> <PostBuildEvent Condition=" '$(OS)' == 'Unix' ">cp -a "$(ProjectDir)../Project1/bin/Debug/Models/." "$(ProjectDir)bin/Debug/" </PostBuildEvent> </PropertyGroup>
Step3 - Update your connection string
from
<add name="EntityframeworkTestEntities" connectionString="metadata=res://*/EntityFrameworkTestModel.csdl|res://*/EntityFrameworkTestModel.ssdl|res://*/EntityFrameworkTestModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=EntityframeworkTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
to
<add name="EntityframeworkTestEntities" connectionString="metadata=EntityFrameworkTestModel.csdl|EntityFrameworkTestModel.ssdl|EntityFrameworkTestModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=EntityframeworkTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
来源:https://stackoverflow.com/questions/35547289/entity-framework-metadata-artifact-not-embeded-when-using-xbuild-and-mono