Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc EF代码首先不为n:m关系生成联接表_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

Asp.net mvc EF代码首先不为n:m关系生成联接表

Asp.net mvc EF代码首先不为n:m关系生成联接表,asp.net-mvc,entity-framework,ef-code-first,Asp.net Mvc,Entity Framework,Ef Code First,我一直在使用“代码优先”作为指南n:m。我通常先使用db,但对于学校项目,我必须先使用代码 我的两门课如下: public class Product { public Product() { Stocks = new HashSet<Stock>(); ServingUnits = new HashSet<Unit>(); Orders = new HashSet<Order>(); }

我一直在使用“代码优先”作为指南n:m。我通常先使用db,但对于学校项目,我必须先使用代码

我的两门课如下:

public class Product
{
    public Product()
    {
        Stocks = new HashSet<Stock>();
        ServingUnits = new HashSet<Unit>();
        Orders = new HashSet<Order>();
    }
    public int ProductId { get; set; }

    [StringLength(128, MinimumLength = 1)]
    [Required]
    public string Title { get; set; }

    [StringLength(512, MinimumLength = 1)]
    public string Description { get; set; }

    public int ExperationPeriod { get; set; }

    public byte[] Image { get; set; }

    [Required]
    public DateTime DateCreated { get; set; }

    //foreign keys
    [Required]
    public int StockUnitId { get; set; }

    [Required]
    public string CreatedById { get; set; }

    [Required]
    public int ProductStateId { get; set; }

    [Required]
    public int ProductTypeId { get; set; }

    //navigation properties

    [ForeignKey("StockUnitId")]
    public virtual Unit StockUnit { get; set; }

    [ForeignKey("CreatedById")]
    public virtual ApplicationUser CreatedBy { get; set; }

    [ForeignKey("ProductStateId")]
    public virtual ProductState ProductState { get; set; }

    [ForeignKey("ProductTypeId")]
    public virtual ProductType ProductType { get; set; }

    public virtual ICollection<Stock> Stocks { get; set; }
    public virtual ICollection<Unit> ServingUnits { get; set; }
    public virtual ICollection<Order> Orders { get; set; }

}
公共类产品
{
公共产品()
{
Stocks=newhashset();
ServingUnits=newhashset();
Orders=新HashSet();
}
public int ProductId{get;set;}
[StringLength(128,最小长度=1)]
[必需]
公共字符串标题{get;set;}
[StringLength(512,最小长度=1)]
公共字符串说明{get;set;}
public int exportionperiod{get;set;}
公共字节[]映像{get;set;}
[必需]
public DateTime DateCreated{get;set;}
//外键
[必需]
public int StockUnitId{get;set;}
[必需]
公共字符串CreatedById{get;set;}
[必需]
public int ProductStateId{get;set;}
[必需]
public int ProductTypeId{get;set;}
//导航属性
[ForeignKey(“StockUnitId”)]
公共虚拟单元StockUnit{get;set;}
[ForeignKey(“CreatedById”)]
公共虚拟应用程序用户由{get;set;}创建
[ForeignKey(“ProductStateId”)]
公共虚拟ProductState ProductState{get;set;}
[外键(“ProductTypeId”)]
公共虚拟ProductType ProductType{get;set;}
公共虚拟ICollection存储{get;set;}
公共虚拟ICollection服务单元{get;set;}
公共虚拟ICollection订单{get;set;}
}
另一方面:

public class Unit
{
    public Unit()
    {
        Orders = new HashSet<Order>();
        ProductsStock = new HashSet<Product>();
        ProductsServe = new HashSet<Product>();
    }
    public int UnitId { get; set; }

    [StringLength(255, MinimumLength = 1)]
    [Required]
    public string Title { get; set; }

    [StringLength(128, MinimumLength = 1)]
    public string ShortName { get; set; }
    public string Description { get; set; }

    [Required]
    public double BasisFactor { get; set; }

    [Required]
    public DateTime DateCreated { get; set; }

    //foreign keys
    [Required]
    public string CreatedById { get; set; }

    //navigation properties    
    [ForeignKey("CreatedById")]
    public virtual ApplicationUser CreatedBy { get; set; }

    public virtual ICollection<Product> ProductsStock { get; set; }
    public virtual ICollection<Product> ProductsServe { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}
公共类单元
{
公共单位()
{
Orders=新HashSet();
ProductsStock=新哈希集();
ProductsServe=newhashset();
}
public int UnitId{get;set;}
[StringLength(255,最小长度=1)]
[必需]
公共字符串标题{get;set;}
[StringLength(128,最小长度=1)]
公共字符串短名称{get;set;}
公共字符串说明{get;set;}
[必需]
公共双基本因子{get;set;}
[必需]
public DateTime DateCreated{get;set;}
//外键
[必需]
公共字符串CreatedById{get;set;}
//导航属性
[ForeignKey(“CreatedById”)]
公共虚拟应用程序用户由{get;set;}创建
公共虚拟ICollection ProductsStock{get;set;}
公共虚拟ICollection ProductsServe{get;set;}
公共虚拟ICollection订单{get;set;}
}
但是联接表在我的数据库中不存在。有人能看出我做错了什么吗


谢谢

可能是因为您有两套产品在单元模型中。为什么会这样?是的,您需要一些流畅的代码来定义这些关系@史蒂夫格林-谢谢你。你能把它加在答题格式上吗?这样我就可以接受了?谢谢,这不是一个真正的答案,但是如果你成功了,把你自己的答案贴出来让别人看。