dbcontext

Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily

纵饮孤独 提交于 2019-12-20 04:10:21
问题 This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show using (SqlConnection cn = new SqlConnection(_connectionString)) { cn.Open(); //Code doing something here... cn

How DbContext initializes automatic DbSet<T> properties?

若如初见. 提交于 2019-12-20 01:43:41
问题 Consider the following class: class MyContext : DbContext { public DbSet<Order> Orders { get; set; } } and instantiating a new object: var mycontext = new MyContext(); Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object. 回答1: From looking at the reflected code, when the DbContext

DbContext caching

我的梦境 提交于 2019-12-19 11:42:41
问题 I know the caching of DbContext is not good idea. But I would like to do it fine. What do you think about this way? public class Context : DbContext { private Context() { } private static WeakReference<Context> _cachedContext; public Context Instance { get { Context context; if (!_cachedContext.TryGetTarget(out context)) { context = new Context(); _cachedContext.SetTarget(context); } return context; } } } This code is planned to be used without IDisposable.Dispose calling in the client-side.

DbContext caching

冷暖自知 提交于 2019-12-19 11:42:03
问题 I know the caching of DbContext is not good idea. But I would like to do it fine. What do you think about this way? public class Context : DbContext { private Context() { } private static WeakReference<Context> _cachedContext; public Context Instance { get { Context context; if (!_cachedContext.TryGetTarget(out context)) { context = new Context(); _cachedContext.SetTarget(context); } return context; } } } This code is planned to be used without IDisposable.Dispose calling in the client-side.

Ninject scoping for DBContext used in Quartz.Net job

穿精又带淫゛_ 提交于 2019-12-19 03:13:12
问题 What's the best scoping to use for a DbContext implementation that gets instantiated via Ninject dependency resolver during execution of a Quartz.Net job implementation? If I used thread scope, will the same instance of DbContext get served if the same thread in Quartz's thread pool is used to execute the job multiple times? I would like a scope that means I get one (and only one) new instance of the DbContext each time the job is fired. 回答1: Yes, i would advise against using InThreadScope .

Multi-tenancy web application with filtered dbContext

≡放荡痞女 提交于 2019-12-18 12:04:29
问题 I am new to ASP.Net MVC and multi-tenancy web application. I have done lots of reading, but being a beginner I just follow what I understand. So I managed to built a sample scenario web application and need to solve the ending part of it. Hope this scenario will be useful for some other beginners as well, but would welcome any other approach. Thanks in advance 1) Database in SQLServer 2008. 2) Data layer: C# class library project called MyApplication.Data public class AppUser { [Key] public

dynamic table name in Entity

倖福魔咒の 提交于 2019-12-18 09:37:06
问题 I want to call method with Table name like db.users.select(x=> x.FirstName) , but i made a mistake somewhere , i write like this but this returns detail about table on one row , not return table data which i want, help please public void LoadToGrid(string dbTableName) { dataGridView1.DataSource = db.GetType().GetMember(dbTableName).ToList(); } 回答1: Your question isn't very clear, but if you mean that You wanna do it in a generic way , then yes that is possible. You can take advantage of the

How to Moq Entity Framework SqlQuery calls

杀马特。学长 韩版系。学妹 提交于 2019-12-18 03:05:31
问题 I've been able to mock DbSet 's from entity framework with Moq using this link. However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is being called. Below is what I am trying to mock. var myObjects = DbContext.Database .SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", "some_value") .ToList(); I currently haven't tried anything as did not know how to start mocking this example.

What does the DBContext.Entry do?

 ̄綄美尐妖づ 提交于 2019-12-18 02:02:11
问题 [HttpPost] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } This action receives a movie model and updates it in the database. But I can't figure out how. The movie object isn't attached to the db, so how does entity framework know which row in the db should be updaed? I am sure that the Entry method has something to do with it, but I don't really know

How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

爷,独闯天下 提交于 2019-12-17 18:58:37
问题 I'm trying to work with EF CodeFirst under Oracle with ODP.net. This is my DbContext class: public class MyCEContext : DbContext { public DbSet<Person> Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Person>().ToTable("PERSONS","myce"); } public MyCEContext() : base(new OracleConnection( "Data Source=cebd; User ID=myce; Password=****;"), true) {} } Problem is that when I try to do something like this: MyCEContext context = new