Entity Framework无法加载指定元数据资源问题求助
Alright, let's dig into this issue you're facing. First, let's confirm your initial hunch: yes, if SaveChanges is successfully updating the database, your core database connection string is definitely valid—EF can talk to SQL Server just fine for write operations. The metadata error is a separate problem related to how EF locates your model's schema definition files (CSDL, SSDL, MSL) which it needs for read/query operations.
Here are the most common fixes to resolve this System.Data.MetadataException:
Double-check the metadata path in your connection string
EF connection strings include ametadatasegment that points to your model files. It usually looks like this:metadata=res://*/YourModelName.csdl|res://*/YourModelName.ssdl|res://*/YourModelName.msl;Make sure the
YourModelNamehere exactly matches the actual name of your EF model file (case-sensitive on some systems). If you renamed your model, moved files between projects, or refactored, this path might be outdated. If the wildcard (*) isn't working, try using the full assembly name instead:metadata=res://YourProjectAssemblyName/YourModelName.csdl|res://YourProjectAssemblyName/YourModelName.ssdl|res://YourProjectAssemblyName/YourModelName.msl;Ensure model files are set to Embedded Resource
In Visual Studio, right-click each of your CSDL, SSDL, and MSL files, go to Properties, and confirm their Build Action is set to Embedded Resource. If they're set toContentor something else, EF won't be able to load them from your compiled assembly.Fix assembly version mismatches (if using a separate class library)
If your EF model lives in a class library project, make sure the main project references the correct version of that library. Sometimes stale references or mismatched assembly versions can cause EF to look for metadata in the wrong place.Clean and rebuild your entire solution
Corrupted build artifacts are a frequent culprit. Do a full clean (Build > Clean Solution) then rebuild (Build > Rebuild Solution). This ensures all embedded resources (your model files) are properly compiled into the assembly.Verify embedded resources exist in the assembly
You can useildasm.exe(included with .NET SDK) to check if your model files are actually embedded. Open the tool, load your compiled DLL/exe, and look under the Manifest section for entries matching your CSDL/SSDL/MSL files. If they're missing, go back and recheck the Build Action setting.
Example Valid Connection String
For reference, here's what a correctly formatted EF connection string should look like (adjust for your server, database, and model details):
<connectionStrings> <add name="YourDbContextName" connectionString="metadata=res://*/YourModel.csdl|res://*/YourModel.ssdl|res://*/YourModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=YOUR_SQL_SERVER;Initial Catalog=YOUR_DATABASE;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings>
内容的提问来源于stack exchange,提问作者Heisenberg

