无法将My DB EF 6.2 2 FK编码为一个PK关系(MVC APP)

时间:2019-01-16 13:10:27

标签: sql asp.net-mvc entity-framework

我一直在尝试先进行编码,并将迁移添加到该代码中,但无法理解我在做什么错。 请记住,我正在尝试制作一个MVC应用来放置和购买产品。

这意味着我有两个模型“产品”和“用户”,我已经不知道该如何 将未注册帐户和已注册帐户分开。

我考虑过要创建一个函数,如果未注册,该函数可能返回-1,如果注册但不知道如何,则返回帐户的ID。

这是我的代码:

 public class Products
{

    [Required]
    public long Id { get; set; }
    public long UserId { get; set; }
    public long OwnerId { get; set; }
    [ForeignKey("OwnerId")]
    public Users Owner { get; set; }
    [ForeignKey("UserId")]
    public Users User { get; set; }
    public string Title { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    [Required]
    public DateTime Date { get; set; }
    public decimal Price { get; set; }
    [Column("Picture1", TypeName = "image")]
    public byte[] Picture1 { get; set; }
    [Column("Picture2", TypeName = "image")]
    public byte[] Picture2 { get; set; }
    [Column("Picture3", TypeName = "image")]
    public byte[] Picture3 { get; set; }
    public int State { get; set; }
}

我的用户类

public class Users
{
    public long Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public DateTime BirthDate { get; set; }
    [Required]
    public string Email { get; set; }
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Password { get; set; }
}

和DBContext:

public class SiteDBContext : DbContext
{
    public SiteDBContext()
        : base("name =SiteDBContext")
    {

    }

    public DbSet<Users> Users { get; set; }
    public DbSet<Products> Products { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

当我尝试对此进行迁移时,会导致此错误:

  

在表FOREIGN KEY上引入'FK_dbo.Products_dbo.Users_UserId'约束'Products'可能会导致循环或多个级联路径。指定ON DELETE NO ACTIONON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。   无法创建约束或索引。查看以前的错误。

感谢大家,如果这里有一些可以理解的信息,请询问,我会更好地解释。 再次感谢!

CreateTable(
            "dbo.Products",
            c => new
                {
                    Id = c.Long(nullable: false, identity: true),
                    OwnerId = c.Long(nullable: false),
                    UserId = c.Long(nullable: false),
                    Title = c.String(),
                    ShortDescription = c.String(),
                    LongDescription = c.String(),
                    Date = c.DateTime(nullable: false),
                    Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                    Picture1 = c.Binary(storeType: "image"),
                    Picture2 = c.Binary(storeType: "image"),
                    Picture3 = c.Binary(storeType: "image"),
                    State = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
            .ForeignKey("dbo.Users", t => t.OwnerId, cascadeDelete: true)
            .Index(t => t.OwnerId)
            .Index(t => t.UserId);

这是创建的迁移,并且仍然无法以可能导致循环或多个级联路径的错误更新数据库。任何人?

0 个答案:

没有答案