Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all, I am having some issues with my code for deleting a customer. I am getting an error "Cannot implicitly convert type 'SiteFoundation.StoredataAccess.Entities.Customer' to 'SiteFoundation.StoreDataAccess.StoreEntities' ". Below is my code. I am of the belief that it is something to do with ID but not sure.

Thanks for your help!

What I have tried:

Controller:
C#
public string DeleteCustomer(int ID)
{
    using (StoreEntities db = new StoreEntities())
    {
        StoreEntities Customer = db.CustomerSet.Find(ID);
        db.CustomerSet.Remove(Customer);
        db.SaveChanges();
        return "Deleted successfully";
    }
}


StoreEntities
C#
using SiteFoundation.StoreDataAccess.Configurations;
using SiteFoundation.StoreDataAccess.Entities;
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SiteFoundation.StoreDataAccess
{
    public class StoreEntities : DbContext
    {
        public StoreEntities() : base("DefaultConnection")
        {

        }

        #region Store Entities
        public IDbSet<Customer> CustomerSet { get; set; }
        public IDbSet<Order> OrderSet { get; set; }
        public IDbSet<Product> ProductSet { get; set; }
        public IDbSet<OrderDetails> OrderDetailSet { get; set; }
        public IDbSet<Category> CategorySet { get; set; }
        public IDbSet<CategoryDetails> CategoryDetailsSet { get; set; }
        #endregion

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Configurations.Add(new CustomerConfiguration());
            modelBuilder.Configurations.Add(new OrderConfiguration());
            modelBuilder.Configurations.Add(new ProductConfiguration());
            modelBuilder.Configurations.Add(new OrderDetailsConfiguration());
            modelBuilder.Configurations.Add(new CategoryConfiguration());
            modelBuilder.Configurations.Add(new CategoryDetailsConfiguration());
        }
    }

    public class CustomerEntity : DbContext
    {
        public CustomerEntity() : base("DefaultConnection")
        {

        }

    }
}


Model Customer

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SiteFoundation.StoreDataAccess.Entities
{
    public class Customer : IEntity
    {
        public Customer()
        {
            Orders = new List<Order>(); 
        }

        public int ID { get; set; }
        public string CustomerName { get; set; }
        public string CustomerEmail { get; set; }
        public string CustomerPhone { get; set; }
        public string CustomerCountry { get; set; }
        public string CustomerImage { get; set; }

        public virtual ICollection<Order> Orders { get; set; }
    }

}
Posted
Updated 5-Aug-18 16:37pm
v2

1 solution

Actually you are trying to convert a Data Entity to DataContext
I think your code would be something like

public string DeleteCustomer(int ID)
{
   using (StoreEntities db = new StoreEntities())
   {
       Customer customer = db.CustomerSet.Find(ID);
       db.CustomerSet.Remove(customer);
       db.SaveChanges();
       return "Deleted successfully";
   }
}

In above code Customer customer = db.CustomerSet.Find(ID);
Customer is your data entity and customer  is an instance of Customer
 
Share this answer
 
Comments
Member 13812021 6-Aug-18 9:34am    
Thank you for the reply, I had to add a reference to the top also. Which I thought I had there already.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900