ef-code-first

Exception of type 'System.ObjectDisposedException'

≯℡__Kan透↙ 提交于 2019-12-21 21:04:06
问题 I have following EF code first code. It is working fine. But when I look for the value of 'club2.Members', it says following: 'club2.Members' threw an exception of type 'System.ObjectDisposedException'. Message is: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. I have not set value for Members property. That’s okay. However it should not cause any exception internally. Is there a way to overcome this exception? Does removal of

SQLite CreateDatabase not supported error

扶醉桌前 提交于 2019-12-21 20:40:47
问题 I'm using Entity Framework 4.2 CF with SQLite, but when i try to launch the application i got "CreateDatabase is not supported by the provider" error. This is my model mapping: protected override void OnModelCreating(DbModelBuilder modelBuilder) { #region NewsMapping modelBuilder.Entity<News>().ToTable("news"); modelBuilder.Entity<News>().HasKey(x => x.Id).Property(x => x.Id).HasColumnName("ID").HasColumnOrder(0); modelBuilder.Entity<News>().Property(x => x.Status).HasColumnName("STATUS");

Two One-to-Many relationships in the same table

橙三吉。 提交于 2019-12-21 20:18:49
问题 I have a table called SystemAccount , which (up until recently) had a MasterAccountID on it that would point to its parent account (obviously an int? ). My client has now told me that there may be sometimes where an account can have 2 parent accounts (none ever have more than that). I've been trying to make the adjustment in my SystemAccount class, but it's not generating the relationship that I want. Here's part of the class code: [ForeignKey("MasterAccount")] public int? MasterAccountID {

SQL Server Express database file auto-creation error in MVC 4 - But i DON'T want to use SQL Server Express

落爺英雄遲暮 提交于 2019-12-21 15:12:54
问题 I've just deployed a new application in ASP.Net MVC 4. I use a SQL Server 2008 R2 (NOT SQL Express). It worked good for the first 10 minutes, then I did a little change in the code and re-deployed it. Now, whenever I try to access a page that uses SimpleMembership, I get this error: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is

Using a custom attribute in EF7 (core) OnModelCreating

走远了吗. 提交于 2019-12-21 11:05:15
问题 I have a DefaultAttribute defined like so: [AttributeUsage(AttributeTargets.Property)] public class DefaultAttribute : Attribute { /// <summary> /// Specifies this property has a default value upon creation. /// </summary> /// <param name="defaultValue">The default value of the property.</param> /// <param name="useAsLiteral">Set to true if the value is <em>not</em> quoted in the DDL.</param> public DefaultAttribute(object defaultValue, bool useAsLiteral = false) { DefaultValue = defaultValue

EF 4.3 Migration - how to produce a downgrade script?

吃可爱长大的小学妹 提交于 2019-12-21 07:37:49
问题 I have an issue which I could not find answer for across the web. I am using CodeFirst EF 4.3.1 Migrations with MsSQL. I have added several migrations and now I want to produce a script for upgrade/downgrade between two migrations. For upgrade I run the following command which successfully reproduces an upgrade script: PM> Update-Database -Script -SourceMigration:"201205161144187_AddPostAbstract" -TargetMigration:"201205161203310_BlogLimitsAndTableRename" However, for downgrade I run the

The term 'Enable-Migrations' is not recognized mvc code first

梦想与她 提交于 2019-12-21 07:20:09
问题 I'm trying to run for the first time in my new mvc 4 application PM> Enable-Migrations –EnableAutomaticMigrations I'm geting: Enable-Migrations : The term 'Enable-Migrations' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Enable-Migrations –EnableAutomaticMigrations What am I missing? I have installed e.f 5. 回答1: Try typing in

EntityFramework Code First FluentAPI DefaultValue in EF6.X

感情迁移 提交于 2019-12-21 06:47:05
问题 How can I set the default value using EntityFramework Code First FluentAPI for bool property? Something like: Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1); 回答1: Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax: AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false)); MSDN for "AddColumn" method 回答2: I'm not sure about a fluent way, but you can

What changes are needed to move an Entity Framework 4.1 Code First application to production?

∥☆過路亽.° 提交于 2019-12-21 05:39:10
问题 As discussed in this thread, Entity Framework Code First is suited for a development environment. So, what steps or changes need to be made to an Entity Framework Code First application to move it over to Production? Migrate database to production server. Change the connection string in Web.config Any changes needed to the DbContext initialization to keep it from monkeying around in a production environment? (Or will it stay out of trouble since the database schema is correct from the

Reflection + Linq + DbSet

╄→尐↘猪︶ㄣ 提交于 2019-12-21 05:27:08
问题 I use EF code-first 4.1. in my application. Now I want to get entities through WCF services using generic types. I'm trying to reflect generic type and invoke the method ToList of DbSet Object. Here is my code: public string GetAllEntries(string objectType) { try { var mdc = Globals.DbConnection.Create(@"some_db_connection", true); // Getting assembly for types var asob = Assembly.GetAssembly(typeof(CrmObject)); // getting requested object type from assembly var genericType = asob.GetType