Mam projektu MVC 4 skonfigurować i generowane wszystkie klasy modelu przy użyciu Entity Framework. Następnie dodałem klasę i nadałem jej taką samą nazwę jak klasa "MyProjectEntities" i uczyniłem ją częściową, dzięki czemu mogę dodawać spersonalizowane metody w tej klasie. Dodałem kilka metod do bazy danych zapytanie w tej klasie i to dobrze. Kiedy wywołuję te metody, mimo że otrzymuję UnintentionalCodeFirstException. Nie jestem pewien, co zrobiłem źle?Entity Framework UnintentionalCodeFirstException
30
A
Odpowiedz
53
Musisz podać poprawny ciąg połączenia. Nie tylko zwykłe ciągi znaków "Data Source =".
Będziesz mieć ciąg połączenia, który zaczyna się od "metadata =" w pliku konfiguracyjnym, użyj tego.
6
Można również przekształcić prosty ciąg połączenia ciąg połączenia bazy pierwszego:
public static string BuildEntityConnectionStringFromAppSettings(string nameOfConnectionString)
{
var shortConnectionString = GetConnectionStringByName(nameOfConnectionString);
// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
// Initialize the connection string builder for the
// underlying provider taking the short connection string.
SqlConnectionStringBuilder sqlBuilder =
new SqlConnectionStringBuilder(shortConnectionString);
// Set the properties for the data source.
sqlBuilder.IntegratedSecurity = false;
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = String.Format("res://*/Application.{0}.Data.Model.{0}Model.csdl|res://*/Application.{0}.Data.Model.{0}Model.ssdl|res://*/Application.{0}.Data.Model.{0}Model.msl", nameOfConnectionString);
return entityBuilder.ToString();
}
Tło: w moim projekcie było wiele ciągów połączenia i chcieliśmy zachować je proste i porównywalne.
Powiązane problemy
- 1. Entity Framework 4 Repository?
- 2. Entity Framework Validation & usage
- 3. Entity Framework Samouczki wideo
- 4. Entity Framework i Równoległość
- 5. Entity Framework Filter Index
- 6. Entity Framework i DbSet
- 7. SQLite z Entity Framework
- 8. Entity Framework bez transakcji?
- 9. Entity Framework lazy ładowanie
- 10. Automatyczna aktualizacja Entity-Framework
- 11. Zachowanie projekcji Entity Framework
- 12. Uchwyt Entity Framework OptimisticConcurrencyException
- 13. Entity Framework zapytanie outofmemoryexception
- 14. Entity Framework i problemem
- 15. Entity Framework Validation
- 16. Entity Framework i MongoDb
- 17. Entity Framework generic
- 18. Entity Framework odświeżania danych
- 19. Entity Framework Virtual Properties
- 20. Entity Framework + POCO
- 21. Entity Framework Circular referencyjny
- 22. Unicode z Entity Framework
- 23. Entity Framework 6 + SQLite
- 24. Entity Framework 6.1.0 SaveChangesAsync
- 25. Entity Framework Query Optimization
- 26. Entity Framework w .NET?
- 27. Entity Framework Kolejność Zawiera
- 28. Entity Framework VS Ado.net
- 29. Entity Framework zerowy obiektu
- 30. Entity Framework: filtr warunkowy