2012-01-23 10 views
6

Ho deciso di spostare Entity Connection String da app.config a codice. Tuttavia, dopo la sua creazione come questo:Entity Framework: impossibile caricare la risorsa di metadati specificata

public static string GetConnectionString() { 
     string connection = ""; 

     SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); 
     sqlBuilder.DataSource = dbServer; 
     sqlBuilder.InitialCatalog = dbInitialCatalog; 

     sqlBuilder.IntegratedSecurity = false; 
     sqlBuilder.UserID = dbUserName; 
     sqlBuilder.Password = dbPasswWord; 
     sqlBuilder.MultipleActiveResultSets = true; 

     EntityConnectionStringBuilder entity = new EntityConnectionStringBuilder(); 
     // entity.Name = "EntityBazaCRM"; 
     entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl"; 

     entity.Provider = "System.Data.SqlClient"; 
     entity.ProviderConnectionString = sqlBuilder.ToString(); 

     connection = entity.ToString(); 

     return connection; 
    } 

Ho un eccezione generata Unable to load the specified metadata resource. in .Designer.cs.

/// <summary> 
    /// Initialize a new EntityBazaCRM object. 
    /// </summary> 
    public EntityBazaCRM(string connectionString) : base(connectionString, "EntityBazaCRM") 
    { 
     this.ContextOptions.LazyLoadingEnabled = true; 
     OnContextCreated(); 
    } 

Se io definisco .Name dentro la mia Entity creatore si getta un'altra eccezione

"Other keywords are not allowed when the 'Name' keyword is specified." (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.", Exception Type = "System.ArgumentException"

So che mi manca qualcosa che devo cambiare in modo che il codice auto generato utilizza la nuova stringa di connessione ma dove cercarlo?

+0

vedere qui. questa è una buona risposta :) modo http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource – Phil

risposta

26

Dopo aver letto this answers articolo e this blog ho cambiato:

entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl"; 

A:

entity.Metadata = "res://*/"; 

e funziona :-)

+1

Sembra buona soluzione, ma c'è alcun danno nel fare questo? –

+0

Non lo so. Lo sto usando da allora fino ad oggi;) – MadBoy

+0

Ha funzionato per me, anche se ho un po 'di preoccupazione sul perché ... – NibblyPig

Problemi correlati