dbcontext

Convert DbContext to Datatable in Code first entity framework

风格不统一 提交于 2019-11-29 02:20:24
Hello I am trying to convert DbContext result to DataTable . I have one class i.e. ClientTemplateModel which inherits DbContext . In this class I have one DbSet object i.e. public virtual DbSet<imagecomment> ImageComments { get; set; } . I am using Code first entity framework . Here is my query. using (ClientTemplateModel context = new ClientTemplateModel(connectionString)) { var result = context.ImageComments.Where(p => p.Dcn == dcn).OrderByDescending(p => p.CommentsDateTime); } Here I am want convert the result into DataTable . How can I convert this? Vishal Sharma you can use Extension

How to Moq Entity Framework SqlQuery calls

筅森魡賤 提交于 2019-11-29 01:19:52
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. The mocking of the DbSet is below and to re-iterate, I can correctly mock returning a DbSet of MyObject

What does the DBContext.Entry do?

不想你离开。 提交于 2019-11-29 01:06:59
[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 what this method does. I read that it provies information but I cannot understand how by just changing the

Using INotifyPropertyChanged with Entity Framework 6 DbContext Generator

社会主义新天地 提交于 2019-11-29 00:10:25
I know I can use ObjectContext instead, but I like the features of DbContext / DbSet. My application isn't large enough to warrant me writing complex view models, so I'd like to implement change notification on the EF generated models directly. How can I achieve this? I've had great success using a NuGet package called PropertyChanged.Fody to get INPC implemented on the entity classes. Just install the package, then add the [ImplementPropertyChanged] attribute to any class and PropertyChanged.Fody will "inject" INPC into the class as part of the build process. For example if you have a

Multiple DbContexts in N-Tier Application

你。 提交于 2019-11-29 00:06:37
I'm creating my first N-Tier MVC application and I've run into a road block with how to manage multiple DbContexts with my database first approach. I have the following layers Presentation Service (WCF) Business Data Access I don't want an entity framework reference in my service layer but I don't see how to create an Interface or something to manage two contexts. I have it working with a single context warpped in a IDatabaseFactory but I can't seem to find an approach to manage two. Below is my UnitOfWork that is created in my Service ctor but every way I look at it I'm still tied to the

How can I log all entities change, during .SaveChanges() using EF code first?

こ雲淡風輕ζ 提交于 2019-11-28 17:31:57
I'm using EF code first . I'm using a base Repository for all my repositories and an IUnitofWork that inject to the repositories, too: public interface IUnitOfWork : IDisposable { IDbSet<TEntity> Set<TEntity>() where TEntity : class; int SaveChanges(); } public class BaseRepository<T> where T : class { protected readonly DbContext _dbContext; protected readonly IDbSet<T> _dbSet; public BaseRepository(IUnitOfWork uow) { _dbContext = (DbContext)uow; _dbSet = uow.Set<T>(); } //other methods } e.g my OrderRepository is like this: class OrderRepository: BaseRepository<Order> { IUnitOfWork _uow;

Most efficiently handling Create, Update, Delete with Entity Framework Code First

青春壹個敷衍的年華 提交于 2019-11-28 16:46:31
问题 Note: I am using Entity Framework version 5 Inside my generic repository, I have Add , Edit and Delete methods as below: public class EntityRepository<T> : IEntityRepository<T> where T : class, IEntity, new() { readonly DbContext _entitiesContext; public EntityRepository(DbContext entitiesContext) { if (entitiesContext == null) { throw new ArgumentNullException("entitiesContext"); } _entitiesContext = entitiesContext; } //... public virtual void Add(T entity) { DbEntityEntry dbEntityEntry =

How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

邮差的信 提交于 2019-11-28 16:46:09
I'm using the DbContext and Code First APIs introduced with Entity Framework 4.1. The data model uses basic data types such as string and DateTime . The only data annotation I'm using in some cases is [Required] , but that's not on any of the DateTime properties. Example: public virtual DateTime Start { get; set; } The DbContext subclass is also simple and looks like: public class EventsContext : DbContext { public DbSet<Event> Events { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Event>().ToTable("Events"); } } The initializer sets

EntitySet System.InvalidOperationException - “the entity type is not part of the model for the current context”

喜欢而已 提交于 2019-11-28 12:21:39
Similar Questions The entity type <classname> is not part of the model for the current context -and- EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context are similar questions but they are "code first" perspective only, with much simpler data models, and address connection string and mapping issues. Please look closely at this one. Symptom // HomeController.cs public ActionResult Index() { var _db = new MealsContext(); var m = _db.Meals.ToList(); var d = _db.Drinks.ToList(); return View(); } Exception is thrown retrieving the Drinks collection:

How do you configure the DbContext when creating Migrations in Entity Framework Core?

喜夏-厌秋 提交于 2019-11-28 12:16:06
Is there way that dependency injection can be configured/bootstrapped when using Entity Framework's migration commands? Entity Framework Core supports dependency injection for DbContext subclasses. This mechanism includes allowing for configuration of data access outside of of the DbContext . For example, the following would configure EF to persist to a SQL server using a connection string retrieved from config.json ServiceCollection services = ... var configuration = new Configuration().AddJsonFile( "config.json" ); services.AddEntityFramework( configuration ) .AddSqlServer() .AddDbContext