简体   繁体   中英

ASP.NET MVC 5 Merging ApplicationUser with DbContext

I'm trying to integrate ASP Identity into my own DbContext but I'm having no luck with it. What I'm trying to achieve is that each Deal that is posted, a user will be assigned to it.

DbContext:

public class ShareDealsContext : IdentityDbContext<ApplicationUser>
{    
    public ShareDealsContext() : base("name=ShareDealsContext")
    {
    }

    // Other DbSets
    public DbSet<Deals> Deals { get; set; }
}

Deals Model:

    [ForeignKey("User")]
    public virtual string UserId { get; set; }

    public virtual ApplicationUser User { get; set; }

Identity Models:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public virtual ICollection<Deals> Deals { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultContext", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I've tried replacing "DefaultContext" to "ShareDealsContext" inside ApplicationDbContext but still does not work. I'm wanting to remove DefaultContext completely and just use my own ShareDealsContext. Any ideas on what I am missing out on? Thank you

Could you replace DefaultContext with connection string name from web.config?

public ApplicationDbContext() : base("CONNECTION_STRING_NAME", throwIfV1Schema: false)

For example,

在此处输入图片说明

Web.config

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM