locked
why is my data model not found ? RRS feed

All replies

  • User-707554951 posted

    Hi roelofw, 

    After download your project, and open it in vs. and try to fetch data from database

    Now it works well as below:

    MegaChallengeEntities1 Class:

      using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        
        public partial class MegaChallengeEntities1 : DbContext
        {
            public MegaChallengeEntities1()
                : base("name=MegaChallengeEntities1")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public virtual DbSet<Price> Prices { get; set; }
        }
    }

    PriceRepository.cs:

     public class PriceRepository
        {
            public List<Price> GetCustomers()
            {
                using (MegaChallengeEntities1 db = new MegaChallengeEntities1())
                {
                    var dbPrices = db.Prices;
                    var dtoPrices = new List<Price>();
    
                    foreach (Price dbPrice in dbPrices)
                    {
                        var dtoPrice = new Price();
    
                        dtoPrice.PriceId = dbPrice.PriceId;
                        dtoPrice.ProductName = dbPrice.ProductName;
                        dtoPrice.ProductPrice = dbPrice.ProductPrice;
                        dtoPrices.Add(dtoPrice);
                    }
                    return dtoPrices;
                }
            }

    Test.aspx:

     <asp:GridView ID="GridView1" runat="server"></asp:GridView>

    CodeBehind:

     protected void Page_Load(object sender, EventArgs e)
            {
                PriceRepository pro = new PriceRepository();
                //MegaChallengeEntities1 db = new MegaChallengeEntities1();
                //var dbPrices = db.Prices.ToList();
                GridView1.DataSource = pro.GetCustomers();
                GridView1.DataBind();
            }

    Output:

    Best regards 

    Cathy 

    Tuesday, December 12, 2017 7:47 AM
  • User-275943262 posted

    Oke, 

    And then I can use the DTO list so I can find the price of 1 product ? 

    Or can I better rewrite the function for that. 

    Roelof

    Wednesday, December 13, 2017 11:46 AM
  • User-707554951 posted

    Hi roelofw,

    For your problem, you just need to use the code I provided in previous reply.

    It will works for you.

    Best regards

    Cathy
      

    Thursday, December 14, 2017 1:36 AM