简体   繁体   English

来自另一个DbContext的ASP.NET Web API身份属性

[英]ASP.NET Web API Identity property from another DbContext

I have StoreUser identity in my ASP.NET Web API project and StoreDbContext Where I have Product model and DbSet 我在ASP.NET Web API项目和StoreDbContext具有StoreUser身份,在其中具有Product模型和DbSet

this is my Product 这是我的Product

public class Product
{
    public int Id { get; set; }
    public string CodeName { get; set; }
    public decimal Cost { get; set; }
    public decimal Discount { get; set; }
    public int SoldCount { get; set; }
    public string SellerId { get; set; }
    public virtual StoreUser Seller { get; set; }
    public virtual List<ProductImage> Images { get; set; }
    public virtual List<Category> Categories { get; set; }
    public virtual List<ProductText> Texts { get; set; }
}

this is my StoreDbContext 这是我的StoreDbContext

public class StoreDbContext: DbContext
{
    public StoreDbContext() : base("MyCoupon")
    {
        Database.SetInitializer(new StoreDbInitialier());
    }

    public DbSet<Product> Products { get; set; }
    public DbSet<ProductText> ProductTexts { get; set; }
    public DbSet<ProductImage> Images { get; set; }
    public DbSet<CategoryText> CategoryTexts { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Language> Languages { get; set; }
}

and this is my StoreUser class 这是我的StoreUser

public class StoreUser : IdentityUser
{
    public virtual List<Product> Products { get; set; } = new List<Product>();
}

so user can have products 这样用户可以拥有产品

but when I want to add them with 但是当我想添加它们

[Authorize(Roles = "Seller")]
    public IHttpActionResult Post(Product product)
    {
        var userId = HttpContext.Current.User.Identity.GetUserId();
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<StoreUserManager>();

        var user = userManager.FindById(userId);
        product.Seller = user;
        _db.Products.Add(product);
        _db.SaveChanges();
        return Ok(product);
    }

I am getting error 我遇到错误

错误

you need to call, on your StoreDbContext class 您需要在StoreDbContext类上调用

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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