如何扩展应用程序用户以保存订单集合?

时间:2017-12-23 12:45:51

标签: asp.net asp.net-mvc asp.net-identity

我试图扩展应用程序用户(使用Code-First)来保存订单集合,但我收到了错误。

我的订单类是

public class Order
{
    public Order()
    {
        OrderDetails = new HashSet<OrderDetails>();
    }

    public int ID { get; set; }
    public DateTime OrderDate { get; set; }
    public string UserId { get; set; }
    public bool IsDelivered { get; set; }
    public bool IsReturned { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<OrderDetails> OrderDetails { get; set; }
}

我试图像这样扩展应用程序用户

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string CompanyName { get; set; }
    public string Profession { get; set; }
    public string TaxAuthority { get; set; }
    public string TaxNumber { get; set; }
    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string MobilePhone { get; set; }
    public bool NewsLetterSubscribe { get; set; } = false;
    public DateTime TimeStamp { get; set; } = DateTime.Now;

    public  ICollection<Order> Orders { get; set; }
}

我收到以下错误:

  

QCMS.Models.IdentityUserLogin :: EntityType&#39; IdentityUserLogin&#39;没有定义键。定义此EntityType的密钥。

     

QCMS.Models.IdentityUserRole :: EntityType&#39; IdentityUserRole&#39;没有定义键。定义此EntityType的密钥。

     

IdentityUserLogins:EntityType:EntitySet&#39; IdentityUserLogins&#39;基于类型&#39; IdentityUserLogin&#39;没有定义键。

     

IdentityUserRoles:EntityType:EntitySet&#39; IdentityUserRoles&#39;基于类型&#39; IdentityUserRole&#39;没有定义键。

你能帮我解决这个问题吗?

更新

我使用两个db上下文。为个人用户帐户(首次创建项目时)和第二个名为&#34; qvModel&#34;这是我项目的所有其他数据库类。

public partial class qvModel : DbContext
{
    public qvModel()
        : base("name=qvModel")
    {
    }
    //APPSETTINGS
    public virtual DbSet<AdminLog> AdminLog { get; set; }
    public virtual DbSet<WebLog> WebLog { get; set; }
    //LANGUAGES
    public virtual DbSet<Language> Languages { get; set; }

    .
    .
    .
    public virtual DbSet<Order> Orders { get; set; }
    public virtual DbSet<OrderDetails> OrderDetails { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        // Precision attribute for decimals
        Precision.ConfigureModelBuilder(modelBuilder);

        modelBuilder.Entity<Language>()
            .HasMany(e => e.Brochures)
            .WithRequired(e => e.Language)
            .WillCascadeOnDelete(true);
        .
        .
        .

        modelBuilder.Entity<Order>()
            .HasMany(c => c.OrderDetails)
            .WithRequired(c => c.Order)
            .WillCascadeOnDelete(true);

        modelBuilder.Entity<ApplicationUser>()
            .HasMany(c => c.Orders)
            .WithRequired(c => c.User)
            .WillCascadeOnDelete(true);

        base.OnModelCreating(modelBuilder);

    }
}

1 个答案:

答案 0 :(得分:1)

我找到了一个非常简单的解决方案。 解决方案是继承IdentityDbContext,就像这样

class Matrix
{
    int rows, cols;
    public double[,] m; //What should I write here?

    public Matrix() 
    {
        rows = 0;
        cols = 0;
    }

    public Matrix(int rows, int cols)
    {
        this.rows = rows;
        this.cols = cols;
        this.m = new double[rows];
        Random r = new Random();
        for (int i = 0; i < rows; i++)
        {
            this.m[i] = new double[cols];
            for (int j = 0; j < cols; j++)
            {
                this.m[i, j] = r.Next(0, 1000);
            }
        }
    }
}

我也错过了OnModelCreating

中的以下行
public class qvModel : IdentityDbContext<ApplicationUser>
{
    public qvModel()
        : base("name=qvModel")
    {
    }

完成这些更改后,我的迁移工作正常,我不再收到我提到的错误。