dbcontext

Entity Framework 6 - use my getHashCode()

本小妞迷上赌 提交于 2019-12-02 22:53:52
There's a certain amount of background to get through for this one - please bear with me! We have a n-tier WPF application using EF - we load the data from the database via dbContext into POCO classes. The dbContext is destroyed and the user is then able to edit the data. We use "state painting" as suggested by Julie Lerman in her book "Programming Entity Framework: DBContext" so that when we add the root entity to a new dbContext for saving we can set whether each child entity is added, modified or left unchanged etc. The problem we had when we first did this (back in November 2012!) was that

Using multiple DbContexts with a generic repository and unit of work

非 Y 不嫁゛ 提交于 2019-12-02 17:47:59
My application is getting larger and so far I have a single MyDbContext which has all the tables I need in my application. I wish (for the sake of overview) to split them up into multiple DbContext , like MainDbContext , EstateModuleDbContext , AnotherModuleDbContext and UserDbContext . I am unsure how this is done probably as I am right now using dependecy injection (ninject) to place my DbContext on my UnitOfWork class like: kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork<MyDbContext>)); Should I drop this approach with dependency injection and explicit set the DbContext I wish to use

Error multiple objects with the same key DbContext

被刻印的时光 ゝ 提交于 2019-12-02 17:40:21
问题 I am getting an Error "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I used this method in two places. In first place - work correctly, but in second I have error. How decided this problem? My method: public static void UpdateMehod(ModelEntities context, ProcessedFilest processedFiles) { context.Set<ProcessedFiles>().Attach(processedFiles); context.Entry(processedFiles).Property(p => p.ID)

EF (entity framework) usage of “using” statement

我与影子孤独终老i 提交于 2019-12-02 17:08:28
I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where " using " statement is used, i.e. public Item GetItem(long itemId) { using (var db = new MyEntities()) { return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault(); } } Here we create a new instance of DBcontext MyEntities() . We using " using " in order to "ensure the correct use of IDisposable objects." It's only one method in my manager. But I have more than ten of them. Every time I call any method from the manager I'll be using

Multiple connection to DbContext in EFCore and .net Core2.0

杀马特。学长 韩版系。学妹 提交于 2019-12-02 13:25:53
I have facing a problem to connecting to DBcontext with multiple connection. please help me out from this problem , here is me scenario . I have an Angular5 application and Webapi Controllers. I have 4 databases and Each database has different users . When 4 user are trying to connecting to their database at Same time through Webapi controller , that time all users are getting data from single database (instead of getting different database ) . When we try to Connecting one by one then data getting correctly . Here is my sample code for dynamic connection . Context Connectionstring : protected

Error multiple objects with the same key DbContext

徘徊边缘 提交于 2019-12-02 13:24:55
I am getting an Error "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I used this method in two places. In first place - work correctly, but in second I have error. How decided this problem? My method: public static void UpdateMehod(ModelEntities context, ProcessedFilest processedFiles) { context.Set<ProcessedFiles>().Attach(processedFiles); context.Entry(processedFiles).Property(p => p.ID).IsModified = true; context.SaveChanges(); } I create new method: public static void UpdateProtocol

Ninject, DbContext & Windows Service - New Instance Each Time Thread Runs?

旧巷老猫 提交于 2019-12-02 11:17:09
I'm current writing a Windows Service which connects to an existing service layer using Entity Framework (DbContext), and Ninject to inject the Respositories and DbContext instance. This is pretty much working with one caveat - I want a brand new DbContext instance each time the thread runs, whereas at the moment I'm getting the same one for the entire thread lifetime. My binding looks like this: Bind<IDbContext>().To<EnterpriseDbContext>().InThreadScope(); Bind<IUserRepository>().To<UserRepository>().InThreadScope(); // And other repositories And my thread code looks like this: [Inject]

How to link one-to-one relationship when fk is different from pk - EntityFramework

六月ゝ 毕业季﹏ 提交于 2019-12-02 07:16:29
I have a custom users table in my database and i want to create a one-to-one relationship with aspnetusers table so that when i register a new customer, applicationuser class through UserManager should add the username, email, password and school code to the users table and add an fk in its own table. IS there any tutorial/example implementing this kind of scenario? I am trying to add a one-to-one relationship with my users table but the fk is different than pk Users Table [columns] username password school code userId [pk] ASPNETUsers Table [standard columns] Id, [pk] UserName, PasswordHash,

Which one must I use, MyDbContext.Blogs.Add(ablog) or MyDbContext.Add(ablog)?

好久不见. 提交于 2019-12-02 06:43:35
Consider I have a context MyDbContext inherits DbContext of EFCore 2.0. Blogs is a DbSet<Blog> and Blog is an entity model. When I add a new Blog instance, ablog to the Blogs , which one must I use? MyDbContext.Add(ablog); or MyDbContext.Blogs.Add(ablog); ? How about Find ? MyDbContext.Find<Blog>(1); or MyDbContext.Blogs.Find(1); ? Is there any benefit to use one over the other one? Adding directly data via the DbContext is new to the DbContext in Entity Framework Core and have no equivalents in previous version of Entity Framework where the DbContext is available (i.e. EF 4.1 onwards). But

How to correctly implement other entity's reference to Identity user?

时光毁灭记忆、已成空白 提交于 2019-12-02 06:34:32
问题 I'm using Identity which has his own context. public class ApplicationUser : IdentityUser { // some custom fields } public class IdentityContext : IdentityDbContext<ApplicationUser> { //... } Also I have some other entities like this public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} } which are used by my other context public class MyContext :DbContext { public DbSet<Comment> Comments { get; set; } //... other DbSets } Question. I