Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity state error

Tags:

c#

asp.net

I am working on a project at the minute and i am getting an error with entity state modified, i have included the code and highlighted the line of code in bold. This is the error i am getting, could anybody help me please

Error 4 The type or namespace name 'EntityState' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) C:\Users\podge\Desktop\SportsStore\SportsStore\Models\Repository\Repository.cs 35 27 SportsStore

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;


namespace SportsStore.Models.Repository
{

  public class Repository {
    private EFDbContext context = new EFDbContext();

    public IEnumerable<Product> Products {
      get { return context.Products; }
    }


    public IEnumerable<Order> Orders
    {
      get
      {
        return context.Orders
          .Include(o => o.OrderLines
          .Select(ol => ol.Product));
      }
    }

    public void SaveOrder(Order order)
    {
      if (order.OrderId == 0)
      {
        order = context.Orders.Add(order);
        foreach (OrderLine line in order.OrderLines)
        {
          context.Entry(line.Product).State
          **= System.Data.EntityState.Modified;**

        } 
      }
      else
      {
        Order dbOrder = context.Orders.Find(order.OrderId);
        if (dbOrder != null)
        {
          dbOrder.Name = order.Name;
          dbOrder.Line1 = order.Line1;
          dbOrder.Line2 = order.Line2;
          dbOrder.Line3 = order.Line3;
          dbOrder.City = order.City;
          dbOrder.State = order.State;
          dbOrder.GiftWrap = order.GiftWrap;
          dbOrder.Dispatched = order.Dispatched;
        }
      }
      context.SaveChanges();
    }
  }
}
like image 364
user3141831 Avatar asked Jan 11 '23 14:01

user3141831


1 Answers

EntityState belongs to System.Data.Entity namespace, not System.Data. But in your code you have used it like System.Data.EntityState

You already have a using statement for System.Data.Entity namespace on the top part of your file. So you you do not need the fully qualified name. you may simply use EntityState.Modified

Replace

context.Entry(line.Product).State= System.Data.EntityState.Modified;

with

context.Entry(line.Product).State= EntityState.Modified;
like image 105
Shyju Avatar answered Jan 17 '23 14:01

Shyju