dbcontext

EF Core / DbContext > Map custom type as primary key

送分小仙女□ 提交于 2019-12-12 08:53:45
问题 Using the fluent api, how do I map a custom type as the primary key within my OnModelCreating method of the DbContext class? Using EF Core I'm trying to build a model for the follow entity. public class Account { public AccountId AccountId { get; } public string Name { get; set; } private Account() { } public Account(AccountId accountId, string name) { AccountId = accountId; Name = name; } } Where the primary key is the AccountId ; the type is a simple value object like this. public class

How would I pass a constructor parameter at runtime to my dbContext and also register them with Unity as such

两盒软妹~` 提交于 2019-12-12 05:44:33
问题 I am trying to implement all sorts of good stuff like UnitOfWork, Repository, DI. I am using Unity for DI. Here is my dilemma. I have a few (currently 3) databases with identical schema but obviously with different data for business reasons (I will call them GroupDB1, GroupDB2 and GroupDB3). I also have a Master Database (DifferentDB) that has a different schema. My dbcontext need to use different databases for different scenarios at runtime. I have no clue how to put them all to work

Use repository with DbContext in ASP.NET Core Authorize-Attribute: “Cannot access a disposed object”

蹲街弑〆低调 提交于 2019-12-12 01:24:44
问题 For third party authentication, I need a custom Authorize attribute. Here a repository ( SessionManager ) class is required to check if the user is logged in. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class VBAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter { public async void OnAuthorization(AuthorizationFilterContext context) { var sessionManager = (VBSessionManager)context.HttpContext.RequestServices

Retrieve the maximum IDENTITY id using transactionscope

霸气de小男生 提交于 2019-12-12 00:56:50
问题 Here is the code: using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { DBContext.AddtoSomeTable(record); System.Threading.Thread.Sleep(5000); DBContext.SaveChanges(); MAX_ID = (from t in DBContext.SomeTable select t.ID).Max(); scope.Complete(); } As far as I know, before the scope is completed, the MAX_ID should be the new record's ID, which is just inserted. Even if there are two users submitting at the same time, the later one should wait until the

is it possible to share POCO object between two DbContext?

痴心易碎 提交于 2019-12-11 16:32:25
问题 I have some POCO classes which can generally divided into two groups, for example: public class Student { public Student() { this.Courses = new List<Course>(); this.Clubs = new List<Club>(); } public int Id { get; set; } public virtual ICollection<Course> Courses { get; set; } public virtual ICollection<Club> Clubs { get; set; } } and corresponding Course and Club classes, and they all have their own relationships to other classes. The problem is, those two groups are big, they both contains

How to make an interface class out of ApplicationDbContext?

断了今生、忘了曾经 提交于 2019-12-11 12:17:34
问题 I am trying to make an interface out of ApplicationDbContext which extends IdentityDbContext. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext { public ApplicationDbContext() : base("AppContext") { } // Should I add below line? without any unwanted behavior? // public DbSet<ApplicationUser> User { get; set;} public DbSet<Book> Books { get; set; } } My interface will be public interface IApplicationDbContext : IDisposable { // Should I add below

EF4.3 know if context has successfully inserted entity

狂风中的少年 提交于 2019-12-11 11:05:24
问题 I have the following method in a generic base class: public virtual void Insert(TEntity entity) { dbSet.Add(entity); } My service layer uses the Insert method to add new records. Now I would like to be able to return a bool, to make sure that it inserted properly. Something like the following: public virtual int Count { get { return dbSet.Count(); } } public virtual bool Insert(TEntity entity) { int count = this.Count; dbSet.Add(entity); return this.Count == count + 1; } Is there a more

Async calling with multiple contexts

馋奶兔 提交于 2019-12-11 10:47:23
问题 So I have code pseudo-similar to (dependency injected): public DbContextA DBA { get; set; } // SQL Server 1 public DbContextB DBB { get; set; } // SQL Server 2 I'm just now sure how I can most effectively do the following? public Task<ActionResult> MyAction() { var trucks = await DBA.GetTrucksAsync(); var cars = DBA.GetCarsAsync(); var cats = await DBB.GetCatsAsync(); var dogs = DBB.GetDogsAsync(); await TaskEx.WhenAll(cars, dogs); } Unless I'm mistaken this isn't optimal. I think it would be

Creating a Trigger with ExecuteSqlCommand() Throws “Incorrect syntax near the word 'TRIGGER'.”

牧云@^-^@ 提交于 2019-12-11 09:42:25
问题 This seems like I'm missing a simple detail, I just can't figure out what. Having this string deleteTrigger = "IF OBJECT_ID('@p0') IS NOT NULL " + "DROP TRIGGER [@p1] "; string createTrigger = "CREATE TRIGGER [@p0] " + "ON [dbo].[@p1] " + "AFTER DELETE AS " + "BEGIN " + "SET NoCount ON " + "DELETE FROM [dbo].[MyTable] WHERE ID IN (SELECT ID FROM DELETED) " + "END "; object[] parameterList = new object[] {"Tr_SomeTable_AD", "Tr_SomeTable_AD" }; context.Database.ExecuteSqlCommand(deleteTrigger

How to add migration in .NET Core Library project in web API core 1.1

≯℡__Kan透↙ 提交于 2019-12-11 08:48:54
问题 I am new in .net core. I am adding migration in .net core Liabrary project in web API core 1.1. using below code. Add-Migration example1 But it's showing below error: 回答1: If you are using VS Code or other editor: In dotnet command cli type: dotnet ef migrations add InitialCreate obs:InitialCreate is the name of migration, you can give any name. If you are using Visual Studio: Create your database Once you have a model, you can use migrations to create a database. Open the PMC: Tools –> NuGet