dbset

How DbContext initializes automatic DbSet<T> properties?

早过忘川 提交于 2019-12-01 21:29:34
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. From looking at the reflected code, when the DbContext (the base class) is constructed it makes a call to the DbSetDiscoveryService (an internal clasS) - which

Moq DbSet NotImplementedException

狂风中的少年 提交于 2019-12-01 18:48:21
I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m.ElementType).Returns(queryable.ElementType); mockSet.As<IQueryable<A>>().Setup(m => m.GetEnumerator())

DbSet.Cast<TEntity>() Error: Cannot create a DbSet<IEntity> from a non-generic DbSet for objects of type 'Entity'

醉酒当歌 提交于 2019-11-30 14:09:56
Version Info: I am using C# 4.5, Entity Framework 6.0, and MEF. Code and Unit Test I created a Test Project to explain the problem: https://skydrive.live.com/redir?resid=E3C97EC293A34048!2234 Please Open the UnitTest project and try to run TestIfItWorks() unit test. Problem I want to convert a non-generic DbSet to its generic version but I am getting the following exception: InvalidCastException: Cannot create a DbSet<IUser> from a non-generic DbSet for objects of type 'User' : var nonGeneric = context.Set(typeof(User)); var generic = nonGeneric.Cast<IUser>(); //Exception in here The User

Inherits from DbSet<T> with the purposes to add property

混江龙づ霸主 提交于 2019-11-30 10:15:09
Is there a way to inherits from DbSet? I want to add some new properties, like this: public class PersonSet : DbSet<Person> { public int MyProperty { get; set; } } But I don't know how to instantiate it in my DbContext public partial MyContext : DbContext { private PersonSet _personSet; public PersonSet PersonSet { get { _personSet = Set<Person>(); // Cast Error here _personSet.MyProperty = 10; return _personSet; } } } How can I achieve this? I have found an answer that works for me. I declare my DbSet properties as my derived interface in my context, e.g.: IDerivedDbSet<Customer> Customers {

DbSet.Cast<TEntity>() Error: Cannot create a DbSet<IEntity> from a non-generic DbSet for objects of type 'Entity'

淺唱寂寞╮ 提交于 2019-11-29 21:22:33
问题 Version Info: I am using C# 4.5, Entity Framework 6.0, and MEF. Code and Unit Test I created a Test Project to explain the problem: https://skydrive.live.com/redir?resid=E3C97EC293A34048!2234 Please Open the UnitTest project and try to run TestIfItWorks() unit test. Problem I want to convert a non-generic DbSet to its generic version but I am getting the following exception: InvalidCastException: Cannot create a DbSet<IUser> from a non-generic DbSet for objects of type 'User' : var nonGeneric

Inherits from DbSet<T> with the purposes to add property

对着背影说爱祢 提交于 2019-11-29 15:22:03
问题 Is there a way to inherits from DbSet? I want to add some new properties, like this: public class PersonSet : DbSet<Person> { public int MyProperty { get; set; } } But I don't know how to instantiate it in my DbContext public partial MyContext : DbContext { private PersonSet _personSet; public PersonSet PersonSet { get { _personSet = Set<Person>(); // Cast Error here _personSet.MyProperty = 10; return _personSet; } } } How can I achieve this? 回答1: I have found an answer that works for me. I

Looping over derived DbContext DbSets so I can add a ClearAll() extension to my DbContext object

喜欢而已 提交于 2019-11-28 14:31:10
Based on [the difference between Type.IsGenericType and Type.IsGenericTypeDefinition ][1], I gained the piece of information I was missing in order to be able to retrieve a DbContext's DbSets via Reflection. However, I am now stuck somewhere else. In the code sample below, I do succeed in obtaining the DbSet generic instances, but I now want to be able to cast them at compile time, to the specific generic type defintion. dbSet is a real instance of one of the DbSets<...> from my DbContext, but how to cast it to its real type definition ? (Take the 1st loop iteration, dBSet will represent DbSet

Can you get the DbContext from a DbSet?

走远了吗. 提交于 2019-11-27 22:00:23
In my application it is sometimes necessary to save 10,000 or more rows to the database in one operation. I've found that simply iterating and adding each item one at a time can take upwards of half an hour. However, if I disable AutoDetectChangesEnabled it takes ~ 5 seconds (which is exactly what I want) I'm trying to make an extension method called "AddRange" to DbSet which will disable AutoDetectChangesEnabled and then re-enable it upon completion. public static void AddRange<TEntity>(this DbSet<TEntity> set, DbContext con, IEnumerable<TEntity> items) where TEntity : class { // Disable auto

X is a variable but used like a type when trying to cast

亡梦爱人 提交于 2019-11-27 19:32:16
问题 I am passing a string of the name of the entity type I want to query and getting the type based on the string. I want to get the DbSet back and return an IQueryable . The problem is where I am doing (DbSet<tableEntity>) and getting the following error: tableEntity is a variable but used like a type when trying to cast. Is there a way to resolve this? public object GetList(string tableEntity) { Type tableEntity = Type.GetType("TestProject." + typeName + ", TestProject"); var dbObject = (DbSet

NSubstitute DbSet / IQueryable<T>

a 夏天 提交于 2019-11-27 11:28:07
So EntityFramework 6 is a lot better testable then previous versions. And there are some nice examples on the internet for frameworks like Moq, but the case is, I prefer using NSubstitute. I've got the "non-query" examples translated to work with the use of NSubstitute, but I can't get my head around the 'query-test'. How does Moq's items.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider); translate to NSubstitute? I thought something like ((IQueryable<T>) items).Provider.Returns(data.Provider); but that didn't work. I've also tried items.AsQueryable().Provider.Returns(data