如何使用EF6

时间:2015-07-18 06:38:02

标签: c# entity-framework-6

 [DataContract]
   public class Commodity
    {
       [DataMember]
        [Key]
        public long ID { get; set; }

       [DataMember]
       [DataType(DataType.Text)]
       [StringLength(40)]
       public string Name { get; set; }

       [DataMember]
       public virtual List<DailyPrice> ComodityDailyPrices { get; set; }

       [DataMember]
     public virtual List<DailyProduction> CommodityDailyProduction { get;set; }

    }

//==========================//

 [DataContract]
  public class Currency
  {
    [DataMember]
    [Key]
    public long ID { get; set; }

    [DataMember]
    [DataType(DataType.Text)]
    [StringLength(10)]
    public string Name { get; set; }

    [DataMember]
    public virtual List<DailyPrice> DailyPrices { get; set; }

  }

 //==========================//

 [DataContract]
  public class DailyPrice
  {
    [DataMember]
    [Key]
    public long ID { get; set; }

    [DataMember]
    public long CommodityID { get; set; }

    [DataMember]
    //[ForeignKey("CommodityID")]
    public virtual Commodity commodity { get; set; }

    [DataMember]
    public long CurrencyID { get; set; }

    [DataMember]
    public virtual Currency currency { get; set; }

    [DataMember]
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

    [DataMember]
    public double Price { get; set; }
}

 //==========================//

 public bool create(DailyPrice _create)
    {
        using (var context = new DbContext())
        {
            context.DailyPrices.Add(_create);
            context.SaveChanges();
            return true;
        }
    }

//货币,商品和daily -prices之间存在关系。所以在保存Dailyprice时,curriences和commodotes也会重复出现像  对于Commodites   1油   2气   3水   4水。

货币

1 INR 2美元 3欧元 4 SDG 9欧元

请让我知道我在哪里做错了。

0 个答案:

没有答案