-2

如何从实体框架中读取?

我用它来插入项目:

    public void Insert()
    {
        using (HistoryContainer db = new HistoryContainer())
        {
            HistoryData e = new HistoryData();
            e.MP = IpAddress;
            e.Number = OtherPartyNumber;
            e.DateTimeStart = DateTime.Now;
            e.Duration = duration;
            e.TypeDescription = type;
            e.text = e.text;
            db.AddToHistoryDataSet(e);
            db.SaveChanges();
        }
    }

我试过这个来阅读它们:

    public virtual IEnumerable<HistoryContainer> ReadFirst()
    {
        using (HistoryContainer x = new HistoryContainer())
        {
            foreach (var item in HistoryData.where(b => b.MP == IpAddress))
            {
                _history.Add(new HistoryItem(item));
            } 
        }
    }

但它没有用。怎么了?谢谢。

4

2 回答 2

1

你在 foreach 语句中忘记了你的容器

public virtual IEnumerable<HistoryContainer> ReadFirst()
{
    using (HistoryContainer x = new HistoryContainer())
    {
        foreach (var item in x.HistoryData.where(b => b.MP == IpAddress))
        {
            _history.Add(new HistoryItem(item));
        } 
    }
}
于 2012-08-02T07:08:17.233 回答
0
   public virtual HistoryData ReadFirst()
    {
        using (HistoryContainer db = new HistoryContainer())
        {
            return db.HistoryData.Where(x => x.MP == IpAddress).FirstOrDefault();
        }
    }
于 2012-08-02T06:52:55.687 回答