实体框架代码优先,模型之间的关系

时间:2018-11-14 15:01:15

标签: asp.net entity-framework

有没有人会听我说,并帮助我解决我的问题?

我有3个班级(产品,批次和物料):

每个产品都有很多批次,每个批次都有很多物料。

public class Product {
    public int id { get; set; }
    public string name { get; set; } 
}

public class Lot {
    public int id { get; set; }
    public decimal price { get; set; } 
}

public class Rem {
    public int id { get; set; }
    public string note  { get; set; } 
}

1 个答案:

答案 0 :(得分:0)

根据您上面的描述,这是EF中相关实体的一种简单方法。

public class Product {
    public int id { get; set; }
    public string name { get; set; } 

    public Lot LotId {get; set; }
    public ICollection<Lot> Lots { get; set; } 
}

public class Lot {
    public int id { get; set; }
    public decimal price { get; set; }

    public Product ProductId {get; set; }
    public Product Product {get; set; }

    public Rem RemId {get; set; }
    public ICollection<Rem> Rems { get; set; }
}

public class Rem {
    public int id { get; set; }
    public string note  { get; set; } 

    public Lot LotId {get; set; }
    public Lot Lot { get; set; } 

}

和dbcontext类

public class AppDbContext: DbContext
{
 public AppDbContext(DbContextOptions<AppDbContext> options): base(options)
{
}

public DbSet<Product> Products { get; set; }
public DbSet<Lot> Lots { get; set; }
public DbSet<Rem> Rems { get; set; }
}

您可以从relationships in EF

中找到有关EF中关系的进一步说明。