构建Linq查询

时间:2013-08-10 11:52:40

标签: asp.net-mvc entity-framework

我搜索过该网站并搜索过但无济于事。也许我累了,因此在谷歌上搜索错误的东西,我不知道。但我是我的智慧结束,真的可以使用一些帮助或建议。我正在使用ASP.NET MVC 4构建一个旅游网站,我遇到了正确的查询问题,用于从数据库中获取我需要的所有信息。

我有一个Safari类,用于存储有关特定野生动物园的信息。我有另一个类Highlight,它存储有关特定野生动物园的亮点的信息。因此,野生动物园可以在一对多的关系中有几个亮点。我最初在Safari的类型列表中有一个亮点,但即便如此,我仍然无法播种和查询“高亮”的细节,并且没有在Safari表中创建字段“高亮”,所以我做了一些亮点一类。我仍然无法为Highlight播种字段,但由于表格已创建,我可以手动完成(因此我知道该表有数据)。

我希望能够查询数据库,不仅可以查看可用的旅行,还可以查询相关的重点。我还包括Safari,Highlight,SafarisController和Configuration类。

Safari.cs

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Zone { get; set; }
    public string Location { get; set; }
    public int Cost { get; set; }
    public List<Highlight> Highlights { get; set; 

Highlight.cs

    public int Id { get; set; }
    public string Content { get; set; }
    public int SafariId { get; set; }

SafarisController.cs

    private ToursTravelDb db = new ToursTravelDb();
    public ActionResult Index()
    {
        var model = db.Safaris.ToList();
        return View(model);
    }

Configuration.cs

    protected override void Seed(Travel_Site.Models.ToursTravelDb context)
    {
        context.Safaris.AddOrUpdate(
            r => r.Name,
            new Safari 
            { 
                Name = "Tsavo East Safari", 
                Description = "This is a short Kenya safari tour", 
                Location = "Tsavo East National Park", 
                Cost = 1200, 
                Zone = "Nairobi",
                Highlights = new List<string>(new string{
                    "Tsavo East National park", 
                    "Views of local homesteads on safari route.", 
                    "Scenic Tsavo East savannah plains.", 
                    "Elephants, Giraffe, Zebra, Buffalo, Cheetah, Lions etc.", 
                    "Panoramic views of Tsavo East  during  lunch stop."
                })
            },
            new Safari 
            { 
                Name = "Amboseli Overnight Safari", 
                Description = "Camp at the Amboseli National Park. ", 
                Location = "Amboseli National Park", 
                Cost = 1200, 
                Zone = "Nairobi", 
                Highlights = new List<string>(new string{
                    "Amboseli National Park", 
                    "Hippopotatamus", 
                    "Flamingoes"
                })
            }
        );
    }

我很欣赏任何正确方向的帮助或指示。干杯

2 个答案:

答案 0 :(得分:0)

您需要引用Safari而不仅仅是Highlight类中的ID。根据我的经验,如果在开发期间将它们分开,则更容易调试此类关系,尤其是种子内容。

Safari.cs

[Key]
public int SafariId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Zone { get; set; }
public string Location { get; set; }
public int Cost { get; set; }
public List<Highlight> Highlights { get; set;  }

Highlight.cs

[Key]
public int Id { get; set; }
public string Content { get; set; }
[ForeignKey("Safari")]
public int? SafariId { get; set; }
[ForeignKey("SafariId")]
public virtual Safari Safari { get; set; }

Configuration.cs

var safaris = new List<Safari>
    {
        new Safari
        {
            Name = "Tsavo East Safari", 
            Description = "This is a short Kenya safari tour", 
            Location = "Tsavo East National Park", 
            Cost = 1200, 
            Zone = "Nairobi"
        },
        new Safari 
        { 
            Name = "Amboseli Overnight Safari", 
            Description = "Camp at the Amboseli National Park. ", 
            Location = "Amboseli National Park", 
            Cost = 1200
        }
    };
safaris.ForEach(i => context.Safaris.Add(i));
context.SaveChanges();

var highlights = new List<Highlight>
    {
        new Highlight
        {
            Content = "Tsavo East National park",
            Safari = (from s in context.Safaris where Name == "Tsavo East Safari" select e)
        }
    };
highlights.ForEach(i => context.Highlights.Add(i));
context.SaveChanges();

我希望这可以帮助你...

答案 1 :(得分:0)

一些有助于此的事情:

  1. 实体框架可以使用导航属性创建关系并创建相关数据。在Safari.cs中,使用通用集合。例如public virtual ICollection<Highlight> Highlights {get;set;}

  2. 没有必要从高光到野生动物园有导航属性,但它绝对有帮助。

  3. 执行查询时,使用.Include()“急切地”加载导航属性。例如var model = db.Safaris.Include(s => s.highlights).ToList();

  4. 此外,您的种子方法是创建一个字符串列表,而不是一个突出显示列表。