Entity framework 首先使用数据注释编码单向多对多关系
我的应用程序中有两个模型:Entity framework 首先使用数据注释编码单向多对多关系,entity-framework,ef-code-first,Entity Framework,Ef Code First,我的应用程序中有两个模型:Stock和Report。一个报表可以有许多库存,一个库存可以在许多报表中使用 public enum ElectionType { MANAGER =1 , INSPECTOR , BOTH } public class Report { public Report() { Stocks = new List<Stock>(); } public int ReportID {
Stock
和Report
。一个报表可以有许多库存,一个库存可以在许多报表中使用
public enum ElectionType
{
MANAGER =1 ,
INSPECTOR ,
BOTH
}
public class Report
{
public Report()
{
Stocks = new List<Stock>();
}
public int ReportID { get; set; }
[Required]
public DateTime ShamsiDate { get; set; }
public int? StockID { get; set; }
[Required]
public ElectionType ElectionType { get; set; }
[ForeignKey("StockID")]
public virtual ICollection<Stock> Stocks { get; set;}
}
public class Stock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StockID { get; set; }
public int FinancialCode { get; set; }
public String NationalCode { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public String FatherName { get; set; }
public String Place { get; set; }
public int StockCount { get; set; }
public int StockPrice { get; set; }
public int StockSize { get; set; }
public int StartStock { get; set; }
public int EndStock { get; set; }
}
公共枚举选择类型
{
经理=1,
检查员
二者都
}
公开课报告
{
公开报告(
{
股票=新名单();
}
public int ReportID{get;set;}
[必需]
公共日期时间ShamsiDate{get;set;}
public int?StockID{get;set;}
[必需]
公共选举类型选举类型{get;set;}
[外汇(“股票ID”)]
公共虚拟ICollection存储{get;set;}
}
公开股
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int StockID{get;set;}
公共金融代码{get;set;}
公共字符串国家代码{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串父名称{get;set;}
公共字符串Place{get;set;}
public int StockCount{get;set;}
公共整数股票价格{get;set;}
public int StockSize{get;set;}
public int StartStock{get;set;}
公共int-EndStock{get;set;}
}
我想创建一个单向关系,这样就无法从
股票
访问报告
。我已经编写了这段代码,但它不起作用。要使用注释,您需要一个连接表:
public class ReportStock
{
[Key, Column(Order = 1)]
public int ReportID { get; set; }
[Key, Column(Order = 2)]
public int StockID { get; set; }
[ForeignKey("ReportID")]
public virtual Report Report { get; set;}
[ForeignKey("StockID")]
public virtual Stock Stock { get; set;}
}
然后更改报告类:
public class Report
{
public Report()
{
ReportStocks = new List<ReportStock>();
}
public int ReportID { get; set; }
[Required]
public DateTime ShamsiDate { get; set; }
[Required]
public ElectionType ElectionType { get; set; }
public virtual ICollection<ReportStock> ReportStocks { get; set;}
}
公共类报告
{
公开报告(
{
ReportStocks=新列表();
}
public int ReportID{get;set;}
[必需]
公共日期时间ShamsiDate{get;set;}
[必需]
公共选举类型选举类型{get;set;}
公共虚拟ICollection ReportStocks{get;set;}
}
你应该指出你认为是什么代码起作用,以及为什么你认为它“不起作用”。基于什么输入,你期望得到什么输出?