entity-framework-5

Most efficiently handling Create, Update, Delete with Entity Framework Code First

青春壹個敷衍的年華 提交于 2019-11-28 16:46:31
问题 Note: I am using Entity Framework version 5 Inside my generic repository, I have Add , Edit and Delete methods as below: public class EntityRepository<T> : IEntityRepository<T> where T : class, IEntity, new() { readonly DbContext _entitiesContext; public EntityRepository(DbContext entitiesContext) { if (entitiesContext == null) { throw new ArgumentNullException("entitiesContext"); } _entitiesContext = entitiesContext; } //... public virtual void Add(T entity) { DbEntityEntry dbEntityEntry =

Entity Framework Migrations renaming tables and columns

早过忘川 提交于 2019-11-28 16:17:13
I renamed a a couple entities and their navigation properties and generated a new Migration in EF 5. As is usual with renames in EF migrations, by default it was going to drop objects and recreate them. That isn't what I wanted so I pretty much had to build the migration file from scratch. public override void Up() { DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports"); DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups"); DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections"); DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" });

how to create an audit trail with Entity framework 5 and MVC 4

我只是一个虾纸丫 提交于 2019-11-28 15:49:38
I am building an MVC 4 application, using EF 5. I need to do an audit trail, ie log any changes that end users make. I have asked this question a few times, but haven't really gotten a satisfying answer before. So I am adding a lot more details in hoping to get somewhere.. currently I have multiple repositories ie public class AuditZoneRepository : IAuditZoneRepository { private AISDbContext context = new AISDbContext(); public int Save(AuditZone model, ModelStateDictionary modelState) { if (model.Id == 0) { context.AuditZones.Add(model); } else { var recordToUpdate = context.AuditZones

Unit Of Work & Generic Repository with Entity Framework 5

不羁的心 提交于 2019-11-28 15:40:22
I'm using ASP.NET MVC 4 with Entity Framework 5. I have model classes and Entity Maps to map existing tables to those model classes. All this is setup fine and works great. Now I want to mock this. I created Unit Of Work that takes the DataContext and uses a Generic Repository. Upon that I built services to be able to get data from many repositories at once and only needing to have one instance of the DataContext. This also works great. Now to the problem: I want to test the services, with mock data. When I create the Unit Of Work instance, I want to be able to insert a DataContext that is

How do I undo the last Add-Migration command?

梦想与她 提交于 2019-11-28 15:33:54
I have created a migration using the Add-Migration command, but I'd like to change the name of that migration. How can I undo the migration command, so that I can regenerate it using the new desired name? Is it just a matter of deleting the generated files, or this could be a bad idea? If you haven't used Update-Database you can just delete the migration file. If you've run the update you should roll it back using Update-Database -TargetMigration "NameOfPreviousMigration" then delete the migration file. Reference: http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/ If you haven

How check by unit test that properties mark as computed in ORM model?

送分小仙女□ 提交于 2019-11-28 14:46:22
I'm created ORM by Entity Framework 5.0 (C# 4.5) - database first. Some properties of entities i'm marked as computed (binded to columns with defaults). How check by unit test that properties mark as computed in ORM model? Note: test need for control computed properties after emergency recreate entity in ORM. Entity description in *.edmx: <EntityType Name="Users"> <Key> <PropertyRef Name="Identifier" /> </Key> <Property Name="Identifier" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="32" /> <Property Name=

Return column based on record in FK table

半世苍凉 提交于 2019-11-28 14:23:55
I have a success query for displaying Vendors, but what I wanted to add now is a column showing which vendors have been selected or not (displayed as a checkbox column). These selections are stored in VendorsSelected table that contains the FK ProfileID and UserName that selected them. So when the current user views the Vendors there will be matches for some Vendors and not for others. How do I modify the query? Note the Where clause below will accomplish getting only vendors that the current user has selected, but what I want is all vendors displayed with true/false (checkbox) column for each

Cant remove identity attribute from PK

帅比萌擦擦* 提交于 2019-11-28 14:12:21
I need to change the data type of the primary key on one of my tables to string from int (I didn't say I WANT to...). The migration generates this bit of code: AlterColumn("dbo.Venues", "ID", c => c.String(nullable: false, maxLength: 128)); This causes SQL server to complain that the data type must be int, bigint, etc because the column is an identity column. I added the following but it appears to have no effect: AlterColumn("dbo.Venues", "ID", c => c.Int(identity:false)); Question: How do I get the migration to change the data type from int to string? The following may be related to my

Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First

醉酒当歌 提交于 2019-11-28 12:27:39
i tried the whole day to get this working. I learned a lot about EF's Fluent API (e.g. this is an excellent article), however i had no success. I have three Entities: public class Address { [Key] public virtual int AddressId { get; set; } public virtual string AddressString { get; set; } } public class User { [Key] public virtual int UserId { get; set; } public virtual ICollection<Address> Addresses { get; set; } } public class House { [Key] public virtual int HouseId { get; set; } public virtual Address Address { get; set; } } and tried all combinations of HasMany, HasOptional, WithOptional,

Insertion order of multiple records in Entity Framework

家住魔仙堡 提交于 2019-11-28 12:03:55
I'm having trouble with EF reordering my inserts when I try and add an entity with multiple children all at once. I've got a 3 level structure with one-to-many relationships between each ( Outer 1--* Item 1--* SubItem ). If I try and insert a new Outer with Items and Subitems, the Items which contain SubItems end up being inserted first. Sample Code (.NET 4.5, EF 5.0.0-rc): public class Outer { public int OuterId { get; set; } public virtual IList<Item> Items { get; set; } } public class Item { public int OuterId { get; set; } [ForeignKey("OuterId")] public virtual Outer Outer { get; set; }