dbcontext

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

亡梦爱人 提交于 2019-12-02 06:23:58
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.Close(); } So i was thinking about to create a DbContext that can handle the creation, the opening and

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

半腔热情 提交于 2019-12-02 02:12:47
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 want that my Comment entity had author property, so I'll have something like public class Comment{ public

Entity framework DbContext update value without query and by foreign key

烈酒焚心 提交于 2019-12-01 21:37:49
问题 I have a method for updating some tables. For update I need get first of TestProcess , but I don't like that. How can I update TestProcess without select(firstOrDefault) operation, used only for the update operation? Example of method: public void UpdateTestProcess(int id, string updateID) { using (TestEntities context = new TestEntities()) { TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id); pr.UpdateID = updateID; context.TestProcess.Attach(pr); context

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

SqlException: Syntax Error Near 'GO'

南楼画角 提交于 2019-12-01 15:05:35
I am having trouble sending a SQL statement through a DbContext using context.Database.ExecuteSqlCommand() . I am trying to execute CREATE TABLE Phones([Id] [uniqueidentifier] NOT NULL PRIMARY KEY, [Number] [int],[PhoneTypeId] [int]) GO ALTER TABLE [dbo].[Phones] ADD CONSTRAINT [DF_Phones_Id] DEFAULT (newid()) FOR [Id] GO This fails with the error string Incorrect syntax near the keyword 'ALTER'. Incorrect syntax near 'GO'. However running that exact statement in SSMS runs without errors? Any issues I need to resolve regarding the default constraint throught the DbContext. I have see problems

DbContext caching

一笑奈何 提交于 2019-12-01 12:52:10
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. What problems this can cause except singleton (anti)pattern? Thanks. The DbContext is a cache. Keeping

Execute SQL Alter commands for every session with Entity Framework 5 talking to Oracle 11g

馋奶兔 提交于 2019-12-01 08:38:52
I have a requirement to execute some SQL commands at the start of every database session. I am using Entity Framework 5 via DbContext talking to a Oracle 11g database. I would like to execute: ALTER SESSION SET NLS_COMP=ANSI; ALTER SESSION SET NLS_SORT=BINARY_CI; at the start of an session creation to get case insensitive searching. How best could I go about this? I've put the commands in the constructor of dbContext, but have only simple unit test and it does appear to work. But unsure if this is right thing to do public partial class Entities : DbContext { public Entities() : base("name

The entity type is not part of the model, EF 5

此生再无相见时 提交于 2019-12-01 05:24:34
I am trying to update my repository to EF5 but have encountered a few errors. I've taken a look around stackoverflow for similar errors discovered a few questions/answers but unfortunately the same answers didn't resolve my issue. This is my error: The entity type User is not part of the model for the current context. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. This is my DbContext class: public abstract class WebModelContext : DbContext {

Execute SQL Alter commands for every session with Entity Framework 5 talking to Oracle 11g

六月ゝ 毕业季﹏ 提交于 2019-12-01 04:55:39
问题 I have a requirement to execute some SQL commands at the start of every database session. I am using Entity Framework 5 via DbContext talking to a Oracle 11g database. I would like to execute: ALTER SESSION SET NLS_COMP=ANSI; ALTER SESSION SET NLS_SORT=BINARY_CI; at the start of an session creation to get case insensitive searching. How best could I go about this? I've put the commands in the constructor of dbContext, but have only simple unit test and it does appear to work. But unsure if

Entity Framework 4.1 Database First does not add a primary key to the DbContext T4 generated class

强颜欢笑 提交于 2019-12-01 04:12:36
I am just getting started with Entity Framework 4.1, trying out the "database first" mode. When EF generates a Model class with the "ADO.Net DbContext Generator," shouldn't it identify the primary key for the class with a [Key] attribute? Without this, it appears incompatible with the T4 MVCScaffolding. Here are the details: Using the Entity Data Model Designer GUI, I have added a simple "country" table to the model from my existing database. The GUI correctly identifies a single integer identity key field named "PK" as my primary key. (Alas! I'm a new user so I can't add a screenshot. I've