ASP.NET MVC 5将ApplicationUser与DbContext合并

时间:2014-10-09 02:13:50

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

我试图将ASP身份整合到我自己的DbContext中,但我没有运气。我想要实现的是每个已发布的交易,用户都将被分配给它。

的DbContext:

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

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

优惠模式:

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

    public virtual ApplicationUser User { get; set; }

身份模型:

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();
    }
}

我已尝试更换&#34; DefaultContext&#34;到&#34; ShareDealsContext&#34;在ApplicationDbContext中,但仍然无法正常工作。我想完全删除DefaultContext并使用我自己的ShareDealsContext。关于我错过的任何想法?谢谢

1 个答案:

答案 0 :(得分:0)

你可以用web.config中的连接字符串名替换DefaultContext吗?

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

例如,

enter image description here

的Web.config

enter image description here

相关问题