entity-framework-6

The item with identity 'Id' already exists in the metadata collection. Parameter name: item

耗尽温柔 提交于 2019-12-05 06:00:31
All of my entities have a base class: public class Entity<TKey> : IEntity<TKey> { dynamic IEntity.Id { get { return this.Id; } set { this.Id = value; } } public TKey Id { get; set; } } For example Status entity: [MetadataType(typeof(StatusMetadata))] public partial class Status : Entity<byte> { public string Title { get; set; } } When I run the query against the database I get the following error: "The item with identity 'Id' already exists in the metadata collection. Parameter name: item". Is there any way to fix this or it's an issue caused by inheritance and I can't inherit my entities from

Entity Framework using IdentityDbContext with Code First Automatic Migrations table location and schema?

寵の児 提交于 2019-12-05 04:36:42
I am trying to setup automatic migration updates using the IdentityDbContext class and propagating changes to the actual DbContext for the entire database. Before I get into the code, on my implementation of the IdentityDbContext with automatic migrations I get this error: Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported. Please use code-based migrations for operations that affect the location of the migrations history system table. I am not going to post the models that are associated with the migrations

Renaming Identity tables with EF6 Migrations Failing

醉酒当歌 提交于 2019-12-05 04:12:39
I'm trying to rename my Identity 2.0 tables via the Migrations tool in EF6/Package Manager. However, it's blowing up a part of the way through. I'm simply calling the following piece of code after the "ApplicationDBContext Create" in IdentityModels.cs: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>().ToTable("Users"); modelBuilder.Entity<IdentityRole>().ToTable("Roles"); modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles"); modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");

EF6 DbSet<T> returns null in Moq

最后都变了- 提交于 2019-12-05 04:09:17
I have a typical Repository Pattern setup in my application with a DbContext (EF6): public class MyDbContext : EFContext<MyDbContext> { public MyDbContext () { } public virtual DbSet<CartItem> Cart { get; set; } and a repository: public class GenericEFRepository<TEntity, TContext> where TEntity : class, new() where TContext : EFContext<TContext> { private readonly TContext _context; public GenericEFRepository(TContext context) { _context = context; } //... public virtual TEntity Insert(TEntity item) { return _context.Set<TEntity>().Add(item); } I'm testing this with Moq 4.2 (following this

Extract strongly-typed data context instance from arbitrary query

纵饮孤独 提交于 2019-12-05 03:19:49
Background We have a class library which has a grid (inherits from WPF DataGrid) with refresh functionality. The grid has a IQueryable Query property, which enables the refresh. Each grid's query is defined not in the class library, but in the referencing end-project: var dg = new RefreshableDataGrid(); dg.Query = () => new ProjectDbContext().Persons; Each grid also has a textbox for text filtering. When text is entered in the filter, an expression is generated which checks if any string property or string-convertible property (using SqlFunctions.StringConvert ) contains the filter string. The

ASP.NET-Identity limit UserName length

我的未来我决定 提交于 2019-12-05 03:09:21
How can I limit the UserName field in the table AspNetUsers ? Neither this: public class ApplicationUser : IdentityUser { [Required, MaxLength(15)] public string UserName { get; set; } } or this: modelBuilder.Entity<ApplicationUser>().Property(x => x.UserName).HasMaxLength(15); works. I need this because setting an Index on an nvarchar(max) gives me this error msg: Column 'UserName' in table 'dbo.AspNetUsers' is of a type that is invalid for use as a key column in an index. To be verbose, I was trying to set the indexes like this: public override void Up() { CreateIndex("dbo.AspNetUsers",

Entity Framework lazy loading with AsNoTracking()

纵饮孤独 提交于 2019-12-05 02:44:29
We are currently using lazy loading for Entity Framework and running into out of memory exception . The reason why we're running into this exception is because the Linq query loads a lot of data and at latter stages it's using lazy loading to load navigation properties. But because we don't use NoTrackingChanges Entity Framework cache builds up really quickly which results in out of memory error. My understanding with EF is the we should always use NoTrackingChanges on query unless you want to update the returned object from the query. I then tested using NoChangeTracking : var account =

How to retrieve Entity Configuration from Fluent Api

落爺英雄遲暮 提交于 2019-12-05 02:41:09
Using Entity-Framework 6 I'm able to set up the configuration through Fluent Api like this: public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser> { public ApplicationUserConfiguration() { this.HasKey(d => d.Id); this.Ignore(d => d.UserId); } } Source from this question Using the attribute approach I'm able to know what's the property roles by reflection, but I wonder how can I retrieve these configurations, like Key for example, with Fluent Api approach? There's no public property from the EntityTypeConfiguration<> class. Is that possible to get the Key and

Unable to update EntityFramework models from MySQL database in Visual Studio 2015 RC

我的未来我决定 提交于 2019-12-05 02:23:25
My organization upgraded from Visual Studio 2013 to Visual Studio 2015 RC a couple months ago, and we only just now attempted to update some of our existing "db-first" EntityFramework models from our MySQL database. When doing so, we received the following error. An exception of type 'System.ArgumentException' occurred while attempting to update from the database. The exception message is: 'Unable to convert runtime connection string to its design-time equivalent. The libraries required to enable Visual Studio to communicate with the database for design purposes (DDEX provider) are not

How can I use EF6 to update a many to many table

 ̄綄美尐妖づ 提交于 2019-12-05 02:20:16
I have two classes: public partial class ObjectiveDetail { public ObjectiveDetail() { this.SubTopics = new List<SubTopic>(); } public int ObjectiveDetailId { get; set; } public int Number { get; set; } public string Text { get; set; } public virtual ICollection<SubTopic> SubTopics { get; set; } } public partial class SubTopic { public int SubTopicId { get; set; } public string Name { get; set; } } I have an ObjectiveDetail object from the user: var web = { "objectiveDetailId":1, "number":1, "text":"datafromweb", "subTopics":[ {"subTopicId":1, "name":"one" }, {"subTopicId":3, "name":"three", }