entity-framework-6

Call multiple service method in one transaction

随声附和 提交于 2019-12-06 11:46:09
问题 Forgive me I am not good at EF6. If I make some mistake . Please help me to correct it. Thanks. Firstly I want to implement the business logic in my service layer like below. public class userService { void createWithCommit(User user) { MyEntityContext db= new MyEntityContext (); ... db.Users.add(user);//Add a new user entity .... Work w = new Work(); ... db.Works.add(w);//Add a new work entity db.savechanges();//commit the changes which will insert 2 new record . one is user . another is

Object Mapping from stored procedure using the columnname attribute in EntityFramework CodeFirst

不想你离开。 提交于 2019-12-06 11:39:11
问题 I have an existing db that I am using entityframework 6 Code-First on to work with. A requirement though is that all work with the db has to be via stored procedures. So I started out using the mapping that is new in 6.0: modelBuilder.Entity<Post>().MapToStoredProcedures(); The issue is that this only supports mapping Insert, Update, and Delete sp's not the select sp's, I need to be able to us a sp to select. (I do not have access to edit any of the existing sp's) My poco's have attributes on

Entity Framework get user from contex in saveChanges

a 夏天 提交于 2019-12-06 11:27:13
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; } /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> DateTime UpdatedDate { get; set; }

How to parameterize a selector with a function in EF query?

半腔热情 提交于 2019-12-06 11:07:36
问题 I have a projection function that I pass to IQueryable<>.Select() method: private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(){ return e => new PriceItem { Id = e.Id, Price = Math.Round(e.Price, 4) }; } Everything works just fine but I want to parameterize it like that: private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(Func<VendorPrice, decimal> formula){ return e => new PriceItem { Id = e.Id, Price = formula(e) }; } so that I can call it like

Add EF 6.x EntityObject Generator in Visual Studio 2017

核能气质少年 提交于 2019-12-06 10:49:08
We are planning to switch to Visual Studio 2017. For our Entity Framework 6 edmx file we use the EntityObject Generator extension to create us the desired ObjectContext. This extension is only compatible up to VS2013 - inofficially up to VS2015. The solution to simply adjust the manifest file in the vsix does not seem to work for VS2017 though - I guess among others because the vsix architecture changed. Is there a way to get the ObjectContext template without using an old Visual Studio? I assume the template is not always the same for each edmx file so it could simply be copy pasted from an

Error: “The Id field is required.” while registering new user in database with AspNet.Identity 2.0

隐身守侯 提交于 2019-12-06 09:40:17
Welcome, I get the validation error mentioned in topic while creating (registering) new user in my Asp.Net mvc application where I used Asp.Net Identity with my custom ApplicationUser class public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); return userIdentity; } public virtual string EmailConfirmedChar { get; set; } public

How to hook IDbCommandInterceptor only to a specific type of DbContext?

徘徊边缘 提交于 2019-12-06 09:37:30
问题 Currently I'm adding my implementation of IDbCommandInterceptor to the static DbInterception class via a DbConfiguration : public class LogDbConfiguration : DbConfiguration { public LogDbConfiguration() { if (AppConfig.LogDebugLevel > LogDebugLevel.Nothing) { DbInterception.Add(new PerformanceLogDbCommandInterceptor()); } } } And this configuration is added to the context that I'd like to hook, as an attribute: [DbConfigurationType(typeof(LogDbConfiguration))] public partial class

Linq Any always returns true

血红的双手。 提交于 2019-12-06 09:37:18
I have used Linq to Entities for many years, but this is the first time I'm facing this problem. I have Tips and Items tables, which each tip can have many items.I have only 3 items in the database. when editing an Item, I want to make sure that GivenId field is unique for items in having the same Tip. I use this: Item item =db.Items.FirstOrDefault(c => c.Id == id); if (item != null) { if (db.Items .Any(x => x.GivenId == newGivenId && x.Id != item.Id && x.TipId == item.TipId)) { //newGivenId is either different or if it is the same, //it belongs to the same item } else { //newGivenId already

IRepository and Unit Of Work vs Eager Loading

Deadly 提交于 2019-12-06 08:50:35
问题 I know having a Unit Of Work is having an abstraction on top of an abstraction ( DbContext ) and surely that is an anti-pattern, or at least is not necessary. I have the following problem: I have a generic IRepository like so: public interface IGenericRepository<TEntity> where TEntity : class { IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = ""); TEntity GetByID(object id)

How do I use Entity Framework 6 with MySQL in ASP.NET 5?

北战南征 提交于 2019-12-06 07:55:36
问题 I have an existing site that uses ASP.NET MVC 4, Entity Framework 6 and MySQL. I'm trying to upgrade it to ASP.NET 5, but want to continue using Entity Framework 6 as Entity Framework is missing some features and does not yet support MySQL. How do I use EF6 in ASP.NET 5? 回答1: Since Web.config is no longer used with ASP.NET 5, you need to use code-based configuration to configure it instead. To do so, create a new class that inherits from DbConfiguration: public class MyDbConfiguration :