dbcontext

DbContext doesn't give any data

跟風遠走 提交于 2019-12-25 03:39:34
问题 I am using EntityFramework in my ASP .Net MVC project. The problem is I am not getting any data from the Database at all. My Database is a localDB and following is the connection string I am using - <connectionStrings> <add name="PortfolioDBContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=NoobMVC;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> Following is my Db Context implementation - public class PortfolioDBContext : DbContext {

My dbcontext doesn't expect any connection string as an input

旧时模样 提交于 2019-12-25 03:27:54
问题 I have 4 layer in my application UI , DomainClass , Model(DBCntext) , Repository . In repository i have an abstract class like this : public abstract class GenericRepository<C, T> : IGenericRepository<T> where T : class where C : DbContext, new() { private C _entities = new C(); public C Context { get { return _entities; } set { _entities = value; } } public virtual IQueryable<T> GetAll() { IQueryable<T> query = _entities.Set<T>(); return query; } public IQueryable<T> FindBy(System.Linq

How create multiple db context instances by connecting strings?

北慕城南 提交于 2019-12-24 20:25:48
问题 In C#, MVC code first application I have public class CarContext : DbContext { } class in first version of application . And connection string is like <add name="CarContext" providerName="System.Data.SqlClient" Integrated Security=true; connectionString="Data Source=Dragon; Initial Catalog=CarDBv1;"/> When I run application, first version of database is created - CarDBv1 . Then I edit my CarContext class, for example, add new table, change any property etc., also change version of application

DbContext not updating database

末鹿安然 提交于 2019-12-24 19:08:19
问题 When I save changes to DbContext, it doesn't update my database. No errors either. Yes, the incoming form data is filled. Yes, the connection string is correct, I know this because I'm able to retrieve data from the database perfectly fine. If it's of relevance, it's a many-to-many relationship. A roadmap is like an article as far as you're concerned which can be associated with many tags. public static class RoadmapService { static ConkerDbEntities dbContext; public static void

Save changes for one object in WPF [closed]

好久不见. 提交于 2019-12-24 18:43:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . In my application I manage persons information, I have a listBox display list of persons and three buttons Add/Edit/Delete, When I select row and press edit button the application open a new window with person information and save to some database table (ModifyCheck) that this person modifying now

Entity Framework 6 DBContext disable delete option

一个人想着一个人 提交于 2019-12-24 14:26:45
问题 Anyone please help me on how to disable DELETE option in EF6? I mean from the application, now record should be deleted (even accidentally) Thanks. 回答1: Create a user/role in the database that does not have permissions to delete/modify records and use it in your application. EF itself is not meant to be a security tool and there are always options to perform a delete operation (e.g. a developer can send any arbitrary SQL query/command to the database bypassing all the 'security' measures

How to disable Migration Verification during every DbContext initialization

我是研究僧i 提交于 2019-12-24 09:06:50
问题 I am using CodeFirst approach in Entity Framework 6 and running my migration script manually to create Db and later update the changes in the schema. I have noticed that every time the context is initialized , it checks for the " INFORMATION_SCHEMA.TABLES " and " MigrationHistory " metadata in the database. That adds 2 additional queries for each call . This may not be a desirable situation for production environment. Is there a way to set it only once during application startup and these 2

Callback on SaveChanges in EFCore?

五迷三道 提交于 2019-12-24 08:09:13
问题 I'm trying to do something like in my DbContext : public System.Action<IEnumerable<EntityEntry>> OnSave; public override int SaveChanges() { if(OnSave != null) OnSave(ChangeTracker.Entries()); return base.SaveChanges(); } Then DI (AutoFac) the DbContext into another class which can hook into the 'OnSave' function. I'm looking for a single source of truth when this happens. I don't think this will work or be reliable when DbContext is injected into multiple places since I'm pretty sure we end

When Adding Data Annotation Attribute it's throwing Exception

╄→尐↘猪︶ㄣ 提交于 2019-12-24 05:24:10
问题 public class Employee { [Key] public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public decimal Salary { get; set; } public string Email { get; set; } } public class EmployeeContext : DbContext { public DbSet<Employee> Employees { get; set; } } When I'm adding Data Annotation [Required(ErrorMessage = "Employee Name is required")] to the Name property it's throwing an InvalidOperationException . As I was trying to fix the bug I'm getting these

KeyValuePair from running Raw SQL query on Entity Framework

南笙酒味 提交于 2019-12-24 04:22:40
问题 I have the following query and code running and I want the two columns returned on a KeyValuePair. I see the total row returned is right but all the keyvaluepairs are nul ! string query = @"Select id,name from persons"; var persons = context.Database.SqlQuery<KeyValuePair<string,string>>(query); I have seen an answer saying that I have to create a class to get the result; but my question is can't I get the result on KeyValuePair ? Or I must have a class defined with properties matched ? 回答1: