ef-code-first

EF - Default value for new column with automatic migration

我的梦境 提交于 2020-01-14 08:03:46
问题 I use EF code first and automatic migrations. I want to add a new column to my model - a boolean column to present "active" (true) or "inactive" (false). How can I add this column and set a default value ("true") for the rows that already in the DB - with automatic migrations? 回答1: Tamar, you need set default value, see next sample: namespace MigrationsDemo.Migrations { using System; using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up

EF - Default value for new column with automatic migration

蹲街弑〆低调 提交于 2020-01-14 08:03:14
问题 I use EF code first and automatic migrations. I want to add a new column to my model - a boolean column to present "active" (true) or "inactive" (false). How can I add this column and set a default value ("true") for the rows that already in the DB - with automatic migrations? 回答1: Tamar, you need set default value, see next sample: namespace MigrationsDemo.Migrations { using System; using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up

ASP.NET MVC : Context type not found in assembly

别等时光非礼了梦想. 提交于 2020-01-14 04:53:06
问题 This is my first asp.net mvc project. I used EF code first approach and created a separate class library to write my model and context. I also referenced the class library in mvc project. But now after creating few more new models when I try to enable migration, it gives me an error. I also tried all the possible solutions mentioned here : No context type found in the assembly. ASP.NET MVC4 Still problem remains the same. Am I missing something? This is code for Context Class: using System;

Using Oracle with EF code first

北战南征 提交于 2020-01-14 03:04:28
问题 I wrote a project with EF code first 6 and SQL Server 2008R2 for a company, Now the company wants to switch its DB to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 , How could I find appropriate version of ODP.NET or ODAC ? Could I use ODAC 12c or any other versions for Oracle 10g or each Oracle version has its own ODAC version? 回答1: You could just rely on the last version of odp.net managed driver but beware that some features might not be available on your 10g (like APPLY)

Error when Update-Database using Code-First: “There is already an object named 'something' in the database.”

霸气de小男生 提交于 2020-01-14 02:30:53
问题 I'm doing database migration using Code-First in Visual Studio 2015. Along the way, i've done the migration steps up till Add-Migration . After Add-Migration, i've added this line of code Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of DbContext1, Migrations.Configuration)) in my DbContext constructor to set up Database Initializer since i missed this step previously. After that, i executed "Add-Migration initial -Force" in the Package Manager Console because i fear that this

EF LINQ ToList is very slow

空扰寡人 提交于 2020-01-13 19:32:10
问题 I am using ASP NET MVC 4.5 and EF6 , code first migrations. I have this code, which takes about 6 seconds. var filtered = _repository.Requests.Where(r => some conditions); // this is fast, conditions match only 8 items var list = filtered.ToList(); // this takes 6 seconds, has 8 items inside I thought that this is because of relations, it must build them inside memory, but that is not the case, because even when I return 0 fields, it is still as slow . var filtered = _repository.Requests

Prevent NULL checks in LINQ to Entity Joins

不打扰是莪最后的温柔 提交于 2020-01-13 11:33:40
问题 We have a table called Student . That table has a field called Homeroom , where the value is a room number of the student's homeroom. The value can be null. We have a second table called Staff . That table also has a field called Homeroom to indicate which homeroom the teacher is assigned to. The value can be null. But when the student's Homeroom is null, a Staff record should not be returned. We used to take advantage of the fact that checking two null fields for equality always returns

Prevent NULL checks in LINQ to Entity Joins

北城余情 提交于 2020-01-13 11:33:06
问题 We have a table called Student . That table has a field called Homeroom , where the value is a room number of the student's homeroom. The value can be null. We have a second table called Staff . That table also has a field called Homeroom to indicate which homeroom the teacher is assigned to. The value can be null. But when the student's Homeroom is null, a Staff record should not be returned. We used to take advantage of the fact that checking two null fields for equality always returns

How to observe the Add action of DbSet<T>?

試著忘記壹切 提交于 2020-01-13 10:05:26
问题 I has two classes named Contact and ContactField as following. When the ContactField is added into Contact , I hope to assign SortOrder to ContactField automatically. Do I need to inherit DbSet and customize the Add method ? How to achieve it ? public class Foo { private MyDbContext _db = new MyDbContext(); public void HelloWorld() { Contact contact = ....; //< A contact from database. ContactField field = ....; ///< A new field .... ///< assign other properties into this `field` field

EF5 Code First and RIA Services Silverlight “Object reference not set to an instance of an object” error building client

坚强是说给别人听的谎言 提交于 2020-01-13 10:05:02
问题 I am working on setting up a new project using Code First for entity framework 5 in silverlight using RIA services. I have created a test project due to some issues I have encountered and will post the code below. Namely, I get an 'Object reference not set to an instance of an object' error anytime I attempt to build the silverlight client project which should generate the client proxy classes. Just to be clear, this error is not while running or debugging the application, but when building