ef-code-first

How do I turn off change tracking at the DbContext level in EF 4.1 RC?

柔情痞子 提交于 2019-12-09 17:56:30
问题 I've encountered what seems to be a common problem: I am updating values in my database, but EF is using its original in-memory copy of the object and these changed values are not reflected in the displayed data. I understand why this is, but I can't figure out a way around it. The most common solution seems to be to set MergeOptions.NoTracking to turn off change tracking completely (or use the AsNoTracking() extension method when querying) and force a refresh every time the object is

Entity Framework 4.1 RC: Code First EntityTypeConfiguration inheritance issue

痴心易碎 提交于 2019-12-09 15:30:26
问题 I am trying to use a common EntityTypeConfiguration class to configure the primary key for all of my entities, so that each derived configuration class does not repeat itself. All of my entities implement a common interface IEntity (which says that each entity must have an Id property of type int). My configuration base class looks like this: public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity> where TEntity : class , IEntity { public EntityConfiguration() { HasKey( e

Entity Type Has No Key Defined

匆匆过客 提交于 2019-12-09 14:57:34
问题 Another 'Entity Type 'x' has no key defined' question, but I've set the [Key] attribute on a property so I'm a bit confused. Here's my entity and context classes: namespace DoctorDB.Models { public class Doctor { [Key] public string GMCNumber; [Required] public string givenName; [Required] public string familyName; public string MDUNumber; public DateTime MDUExpiry; public string MDUCover; } public class DoctorContext : DbContext { public DbSet<Doctor> Doctors { get; set; } } } When I go to

Entity Framework Code First Many-to-Many relationship and inheritance

此生再无相见时 提交于 2019-12-09 12:14:26
问题 Forgive me if this question has been answered somewhere, I have been having a hard time finding a solution for this problem. I am trying to set up EF Code First on an MVC4 Project. I have a User and Customer that both inherit from Person. I then have a Template object that has a Many-to-Many relationship with Customer and a One-to-Many relationship with User. Here is how I have it set up: MODELS public class Person { [Key] public int PersonID { get; set; } public string LastName { get; set; }

EF Code first migrations not running after deploy to Azure

北战南征 提交于 2019-12-09 11:57:26
问题 I have two folders for my migrations (AuthContext and UserProfileContext), each has their own migration and some custom sql to run afterwards for data migrations and whatnot. This works fine when using package manager console. I Restore from production Run Update-Database -ConfigurationTypeName Migrations.Auth.Configuration Run Update-Database -ConfigurationTypeName Migrations.UserProfile.Configuration Then everything is very happy in the new database, migrations executed data shuffled where

How to set manually an Oracle Connection String in a DbContext

ⅰ亾dé卋堺 提交于 2019-12-09 11:00:28
问题 I have the following connection string: <add name="DataContext" connectionString="DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM" providerName="Oracle.DataAccess.Client"/> My business logic determine I need to read manually the connection string of the database: class MyDbContext: DbContext { public MyDbContext() : base(ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString){} ... } It works properly with Sql Server but when I change to a Oracle Connection string don't works.

Entity Framework Code First Mapping Foreign Key Using Fluent API

醉酒当歌 提交于 2019-12-09 10:00:06
问题 I have the situation where a User can have several addresses. Accordingly, I have an ICollection on my user class. But I also want the user to be able to choose a default address. So I've done the following: public class User { public int Id { get; set; } public int? DefaultAddressId { get; set; } [ForeignKey("DefaultAddressId")] public virtual Address DefaultAddress { get; set; } public virtual ICollection<Address> Addresses { get; set; } //properties were removed for purpose of this post }

Entity Framework Migrations Error - Sequence contains no elements

牧云@^-^@ 提交于 2019-12-09 07:39:56
问题 command: add-migration blahblah -verbose error: Sequence contains no elements I did a few things before getting this error. I made a change to my code-first model but did not run add-migration yet. Then I added an EDMX model to play around with an idea visually. I realized the EDMX model was messing with my code so I removed it. I tried to run add-migration and got "Sequence contains no elements". I upgraded to EF 5 and uninstalled the old Migrations package except for my configurations. Then

How to add a new code-first migration with a newly-generated database?

痴心易碎 提交于 2019-12-09 07:11:40
问题 I've enabled code-first migrations on my entity framework project, and have added several migrations which do things like rename tables. However, I have now deleted the database and caused entity framework to generate a brand new database based on my latest data model. If I try to run: PM> Add-Migration TestMigration ... it tells me that I need to apply the existing migrations first. So I run: PM> Update-Database ... but the trouble is that it's trying to update a database that doesn't need

How to set a collection-property with FKs?

浪子不回头ぞ 提交于 2019-12-09 06:56:00
问题 I have a Business and a Category model. Each Business has many Categories via an exposed collection ( Category is disregarding the Business entity). Now here is my controller-action: [HttpPost] [ValidateAntiForgeryToken] private ActionResult Save(Business business) { //Context is a lazy-loaded property that returns a reference to the DbContext //It's disposal is taken care of at the controller's Dispose override. foreach (var category in business.Categories) Context.Categories.Attach(category