entity-framework-6.1

“The ConnectionString property has not been initialized.” - but only when publishing

泄露秘密 提交于 2019-12-08 02:47:46
问题 I have an MVC5 / EF6.1 website that runs perfectly on my development machine using LocalDb. However, when I publish this to an Azure Website with an Azure SQL Database, I get the following error when doing any database interaction: The ConnectionString property has not been initialized. I've searched all over and can't find the reason that this happens on Azure. The first file the stack trace points to is IdentityModels.cs:45 That contains the following: public class ApplicationDbContext :

EF6 code first: How to load DbCompiledModel from EDMX file on startup?

瘦欲@ 提交于 2019-12-06 22:49:50
问题 I want to reduce startup time in EF6 by caching the DbCompiledModel to disk. It's easy to write the EDMX file for a DbContext: EdmxWriter.WriteEdmx(myDbContext, XmlWriter.Create(@"C:\temp\blah.xml")) And it's easy to pass a DbCompiledModel to the DbContext: var db = new DbContext(connectionString, myDbCompiledModel) However there doesn't seem to be any way to read the EDMX file from disk into a DbCompiledModel! How can I do this? NOTE that I have successfully implemented the solution using

How to programatically set database filename?

随声附和 提交于 2019-12-06 14:52:52
问题 Following this tutorial, I've created my DbConfiguration class: class ImgSigDbConfig : DbConfiguration { public ImgSigDbConfig() { SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0")); } } And told my DbContext to use it: [DbConfigurationType(typeof(ImgSigDbConfig))] class ImgSimContext : DbContext { Which seems to connect fine, but I have no idea where it's storing my data, and I don't like that. I would like it to save everything to a file in the App_Data folder. How can I

How can I change the default max length of string properties in Entity Framework 6?

*爱你&永不变心* 提交于 2019-12-06 09:11:55
问题 By default convention, strings properties in an entity model that are not explicitly given a max length are set to nvarchar(max) in the database. We wish to override this convention and give strings a max length of nvarchar(100) if they are not already explicitly set otherwise. I discovered the PropertyMaxLengthConvention built-in convention, which by its description and documentation would seem to be what I am looking for. However, it either doesn't work or I'm using it wrong or it just

“The ConnectionString property has not been initialized.” - but only when publishing

梦想的初衷 提交于 2019-12-06 04:17:11
I have an MVC5 / EF6.1 website that runs perfectly on my development machine using LocalDb. However, when I publish this to an Azure Website with an Azure SQL Database, I get the following error when doing any database interaction: The ConnectionString property has not been initialized. I've searched all over and can't find the reason that this happens on Azure. The first file the stack trace points to is IdentityModels.cs:45 That contains the following: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } } When

Entity Framework 6.1 Updating a Subset of a Record

半世苍凉 提交于 2019-12-05 12:00:14
问题 I have a view model that encapsulates only some of the database model properties. These properties contained by the view model are the only properties I want to update. I want the other properties to preserve their value. During my research I found this answer which appears to be perfect for my needs however, despite my best efforts, I cannot get the code to work as expected. Here is an isolated example of what I came up with: static void Main() { // Person with ID 1 already exists in

EF6 code first: How to load DbCompiledModel from EDMX file on startup?

天涯浪子 提交于 2019-12-05 02:59:47
I want to reduce startup time in EF6 by caching the DbCompiledModel to disk. It's easy to write the EDMX file for a DbContext: EdmxWriter.WriteEdmx(myDbContext, XmlWriter.Create(@"C:\temp\blah.xml")) And it's easy to pass a DbCompiledModel to the DbContext: var db = new DbContext(connectionString, myDbCompiledModel) However there doesn't seem to be any way to read the EDMX file from disk into a DbCompiledModel! How can I do this? NOTE that I have successfully implemented the solution using the EdmxReader tool in this branched version of EF6: https://github.com/davidroth/entityframework/tree

SetExecutionStrategy to SqlAzureExecutionStrategy with DbMigrationsConfiguration?

ⅰ亾dé卋堺 提交于 2019-12-04 23:07:23
I saw a post today about implementing SqlAzureExecutionStrategy: http://romiller.com/tag/sqlazureexecutionstrategy/ However, all examples I can find of this use a Configuration that inherits from DbConfiguration. My project is using EF6 Code First Migrations, and the Configuration it created inherits from DbMigrationsConfiguration. This class doesn't contain a definition for SetExecutionStrategy, and I can find no examples that actually combine SqlAzureExecutionStrategy (or any SetExecutionStrategy) with DbMigrationsConfiguration. Can this be done? If anyone else comes across this question,

How to programatically set database filename?

感情迁移 提交于 2019-12-04 22:55:20
Following this tutorial , I've created my DbConfiguration class: class ImgSigDbConfig : DbConfiguration { public ImgSigDbConfig() { SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0")); } } And told my DbContext to use it: [DbConfigurationType(typeof(ImgSigDbConfig))] class ImgSimContext : DbContext { Which seems to connect fine, but I have no idea where it's storing my data, and I don't like that. I would like it to save everything to a file in the App_Data folder. How can I specify that from inside my ImgSigDbConfig constructor? (Assume I know nothing about SQL server

How to totally lock a row in Entity Framework

大城市里の小女人 提交于 2019-12-04 17:24:35
问题 I am working with a situation where we are dealing with money transactions. For example, I have a table of users wallets, with their balance in that row. UserId; Wallet Id; Balance Now in our website and web services, every time a certain transaction happens, we need to: check that there is enough funds available to perform that transaction: deduct the costs of the transaction from the balance. How and what is the correct way to go about locking that row / entity for the entire duration of my