ef-code-first

Using mvc-mini-profiler database profiling with Entity Framework Code First

醉酒当歌 提交于 2019-12-17 05:04:13
问题 I'm using the mvc-mini-profiler in my project built with ASP.Net MVC 3 and Entity Framework code-first. Everything works great until I attempt to add database profiling by wrapping the connection in the ProfiledDbConnection as described in the documentation. Since I'm using a DbContext, the way I am attempting to provide the connection is through the constructor using a static factory method: public class MyDbContext : DbContext { public MyDbContext() : base(GetProfilerConnection(), true) { }

Using mvc-mini-profiler database profiling with Entity Framework Code First

风流意气都作罢 提交于 2019-12-17 05:03:19
问题 I'm using the mvc-mini-profiler in my project built with ASP.Net MVC 3 and Entity Framework code-first. Everything works great until I attempt to add database profiling by wrapping the connection in the ProfiledDbConnection as described in the documentation. Since I'm using a DbContext, the way I am attempting to provide the connection is through the constructor using a static factory method: public class MyDbContext : DbContext { public MyDbContext() : base(GetProfilerConnection(), true) { }

Debug code-first Entity Framework migration codes

冷暖自知 提交于 2019-12-17 04:41:13
问题 I'm using Entity Framework code first in my website and I'm just wondering if there is any way to debug the migration codes. You know, like setting breakpoints and stuff like this. I'm using Package Manager Console to update the database using Update-Database . Thanks 回答1: I know that EF Code First Migrations is relatively new tool but don't forget about you are still in .NET. So you can use: if (System.Diagnostics.Debugger.IsAttached == false) { System.Diagnostics.Debugger.Launch(); } After

How do I detach objects in Entity Framework Code First?

泪湿孤枕 提交于 2019-12-17 03:52:33
问题 There is no Detach(object entity) on the DbContext . Do I have the ability to detach objects on EF code first? 回答1: If you want to detach existing object follow @Slauma's advice. If you want to load objects without tracking changes use: var data = context.MyEntities.AsNoTracking().Where(...).ToList(); As mentioned in comment this will not completely detach entities. They are still attached and lazy loading works but entities are not tracked. This should be used for example if you want to load

EF Code First “Invalid column name 'Discriminator'” but no inheritance

久未见 提交于 2019-12-17 03:51:38
问题 I have a table in my database called SEntries (see below the CREATE TABLE statement). It has a primary key, a couple of foreign keys and nothing special about it. I have many tables in my database similar to that one, but for some reason, this table ended up with a "Discriminator" column on the EF Proxy Class. This is how the class is declared in C#: public class SEntry { public long SEntryId { get; set; } public long OriginatorId { get; set; } public DateTime DatePosted { get; set; } public

How to delete and recreate from scratch an existing EF Code First database

帅比萌擦擦* 提交于 2019-12-17 02:59:45
问题 I am using EF Code First with EF 5 in VS 2012. I use PM update-database command and I have a simple seed method to fill some tables with sample data. I would like to delete and recreate my x.mdb. The update history seems to be out of sync. If I comment out all my DBSets in my context, update-database runs with no error but leaves some tables in the DB. As I have no valuable data in the DB it seems to the simplest to reset the all thing. How can I accomplish this? 回答1: If I'm understanding it

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

久未见 提交于 2019-12-17 02:25:32
问题 I'm new to Entity Framework. I am trying to setup an MVC Application what uses EF 6. I am using Code First Migrations. I am using Areas in the app and would like to have different DbContexts in each area to break it up. I know EF 6 has ContextKey, but I can't find complete information on how to use it. Currently I can only use migrations one context at a time. Can someone give an example with enough detail for a new person to EF like me to understand and use. 回答1: Entity Framework 6 added

One DbContext per request in ASP.NET MVC (without IOC container)

允我心安 提交于 2019-12-16 22:35:17
问题 Apologies if this has already been answered, but how do you guarantee one Entity Framework DbContext per request if you are not using an IOC container? (The answers I've come across so far deal with IOC container solutions.) It seems like most solutions hook into the HttpContext.Current.Items dictionary, but how do you guarantee disposal of the DbContext when the request is finished? (Or is disposal not absolutely necessary with an EF DbContext ?) Edit I'm currently instantiating and

How can I automatically filter out soft deleted entities with Entity Framework?

北慕城南 提交于 2019-12-16 22:12:43
问题 I am using Entity Framework Code First. I override SaveChanges in DbContext to allow me to do a "soft delete": if (item.State == EntityState.Deleted && typeof(ISoftDelete).IsAssignableFrom(type)) { item.State = EntityState.Modified; item.Entity.GetType().GetMethod("Delete") .Invoke(item.Entity, null); continue; } Which is great, so the object knows how to mark itself as a soft delete (In this case it just sets IsDeleted to true ). My question is how can I make it such that when I retrieve the

Unable to determine the principle end of the relationship - multiple added entities may have the same primary key

岁酱吖の 提交于 2019-12-16 18:06:58
问题 Sets have Cards and Sets. Here's what I have in my model, using EF Code First: public class Set { // Primitive Properties [Required] [Key] public virtual int SetId { get; set; } // Navigation Properties [Required] public virtual List<Set> Sets { get; set; } // Navigation Properties [ForeignKey("ParentSet")] public int ParentSetId { get; set; } public virtual Set ParentSet { get; set; } } Then for Cards: public class Card { // Primitive Properties [Required] [Key] public virtual int CardId {