entity-framework-6

EF6 Code first “model backing DataContext has changed” error (.mdf Db)

不想你离开。 提交于 2019-12-10 11:45:22
问题 I'm new to the code first methodology. I'm getting this error: "The model backing the 'DataContext' context has changed since the database was created. Consider using Code First Migrations to update the database". I have a WPF app using EF6 & MVVM running against a local db (.mdf life). I created a new model, called langauges then I added a new DbSet< langauges > collection to my datacontext. When the code tries to instantiate the a datacontext object, I get the error above. What am I missing

what are the circumstances of using .Wait() when calling async methods

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:22:46
问题 I have the following async long running method inside my asp.net mvc-5 web application :- public async Task<ScanResult> ScanAsync(string FQDN) { // sample of the operation i am doing var c = await context.SingleOrDefaultAsync(a=>a.id == 1); var list = await context.Employees.ToListAsync(); await context.SaveChangesAsync(); //etc.. } and i am using Hangfire tool which support running background jobs to call this async method on timely basis, but un-fortuntly the hangefire tool does not support

Many-to-many without an inverse property

霸气de小男生 提交于 2019-12-10 10:53:53
问题 I want to create a many-to-many relationship in EF. Normally I would use the InverseProperty attribute, however, in this case: Role ( Id, Name, ICollection<Privilege> ) Privilege ( Id, Name ) I don't have an inverse property. How to tell EF that a privilege may be used in more than one Role? [ Currently EF puts a Role_Id column in the Privilege table. This is not what I want :-) ] Edit: I do not want to use the Fluent API, I'm looking for an attribute. 回答1: modelBuilder.Entity<Role>()

Entity Framework 6.1 : The given key was not present in the dictionary

南楼画角 提交于 2019-12-10 10:44:42
问题 I have a table with some relations, the program works fine until I add a new relation between this table and customer table, the ddl for PermissionCode Table (first table) is as below: CREATE TABLE [dbo].[PermissionCode] ( [Id] int NOT NULL IDENTITY(1,1) , [Salt] varchar(3) COLLATE Turkish_CI_AS NOT NULL , [Code] nvarchar(12) COLLATE Turkish_CI_AS NOT NULL , [StartDate] date NULL , [EndDate] date NULL , [TypeId] int NOT NULL , [UserId] nvarchar(128) COLLATE Turkish_CI_AS NULL , [OwnerId] int

Entity Framework get user from contex in saveChanges

…衆ロ難τιáo~ 提交于 2019-12-10 10:42:55
问题 i have two projects in my solution, UI as mvc and class project for entitiy model code first. I have severall entities in my model but now I need to extend them by new audit fields where I need to save who changed entity. I added new interface public interface IAuditable { /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> DateTime CreatedDate { get; set; } /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> string CreatedBy { get; set; } ///

create entity with both primary key and identity column in EF6

坚强是说给别人听的谎言 提交于 2019-12-10 10:12:43
问题 I already have a table with a varchar Primary Key. This is working fine with my current .edmx. Now I added an auto-increament identity column in that table. While I try to update the .edmx, that table is not being included in .edmx Can't I put a varchar PK column and an auto-increament identity column in the same table? 回答1: Yes, here's an example. The identity property has nothing to do with the PK. It just won't ever be null, obviously, and increments from the seed based off what ever you

Update Many-to-Many Association with GraphDiff

瘦欲@ 提交于 2019-12-10 10:06:04
问题 I have the following data model: My business logic works with detached entities so I'm using GraphDiff to perform updates. I'm having trouble updating the PerfModes/CalcPoints association. Conceptually, Block owns CalcPoints and PerfModes, but CalcPoints can be associated with any number of PerfModes. I'm trying to do updates at the Block level. The code I came up with doesn't throw any errors (while other attempts did) but neither does it update the PerfModes/CalcPoints association.

How to specify the shape of results with WebApi2, OData with $expand

半世苍凉 提交于 2019-12-10 09:07:29
问题 I have a problem executing my AutoMapper mappings when using OData with specific $select or $expand values. Using the WebApi Action: public IQueryable<BookInRequestDto> Get(ODataQueryOptions<BookInRequest> query) { var results = query.ApplyTo(_context.BookInRequests) as IQueryable<BookInRequest>; var mappedResults = Mapper.Map<IQueryable<BookInRequest>, IQueryable<BookInRequestDto>>(results); return mappedResults; } When I query: api/Get , I get an appropriate response, but a Document's

EF6 DbSet<T> returns null in Moq

我只是一个虾纸丫 提交于 2019-12-10 03:24:39
问题 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

Renaming Identity tables with EF6 Migrations Failing

↘锁芯ラ 提交于 2019-12-10 03:06:08
问题 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