Entity Framework returns 0 items

c# entity-framework-6

Question

I`m using Entity Framework 6.1.3.

When i try to get values from database table it returns me 0 items but in database are 9 rows.

And Entity FrameWork invokes the OnModelCreating method.

I searched all internet but nothing found how to fix that.

My DbContext class

namespace TestTask.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class Entities : DbContext
    {
        public Entities()
            : base("MenuItemsContainer")
        { }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) 
        { 

        modelBuilder.Properties() 
                    .Where(p => p.Name == "Id") 
                    .Configure(p => p.IsKey()); 
        } 

        public virtual DbSet<DataMenuItemSet> DataMenuItemSet { get; set; }
        public virtual DbSet<ItemsSet> ItemsSet { get; set; }
    }
}

My DataMenuItemSet class

using System.ComponentModel.DataAnnotations;

namespace TestTask.Models
{
    using System;
    using System.Collections.Generic;

    public partial class DataMenuItemSet
    {
        public DataMenuItemSet()
        {
            this.ItemsSet = new HashSet<ItemsSet>();
        }

        [Key]
        public int Id { get; set; }
        public bool IsRoot { get; set; }
        public string Name { get; set; }
        public Nullable<int> Parent { get; set; }

        public virtual ICollection<ItemsSet> ItemsSet { get; set; }
    }
}

All of this is generated with Entity Framework.

I need to get values from database.

Updated

I`ve solved the problem.

The point is that i have two projects in my solution. First is a site, that have the model from database, and second is simple ConsoleApplication, where i tried to test a database data. When i trying to connect to db via dbcontext from the other application its not working as described above. To make it work, I transfered a connection string from Web Site application, that have edmx model and dbcontext, to application, where i were testing this connection and data.

Here is how it works

enter image description here

Yellow - ConsoleApplication Red - Web Site with model and dbcontext

enter image description here

Here is a model and Web.config I transfered the connection string from Web.config to the App.cofig of ConsoleApplication, and the model.

enter image description here

ConsoleApplication with transfered model and connection string.

And after all that it works for me.

Thanks for help !!!

1
5
7/25/2015 7:08:52 PM

Popular Answer

Here is the very common way to get data from db using entity framework and LINQ query.

using (var context = new Entities())
{
    var L2EQuery = from item in context.DataMenuItemSet
                   where item.IsRoot == true
                   select item;

    var result = L2EQuery.ToList()<DataMenuItemSet>;
 }

Hope will be helpful.

0
7/25/2015 4:16:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow