Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I am trying to use Entity framework in my MVC Application. I am trying to add delete update person ('kisi'). Here is my model class.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace Panelium.Models
{
    public class nacikisiBLL
    {
        PaneliumEntities db = new PaneliumEntities();
        //crud
        public void InsertorUpdate(kisi _model)
        {
            if (_model.KisiID > 0)
            {
                // update
                var _update = db.kisi.FirstOrDefault(f => f.KisiID == _model.KisiID);
                if (_update != null)
                {
                    _update.Ad =_model.Ad;
                    _update.Soyadi = _model.Soyadi;
                    _update.Turu = _model.Turu;
                    _update.Eposta = _model.Eposta;
                    db.SaveChanges();
                }
            }
            else
            {
                // insert
                kisi _insert = new kisi
                {
                    Ad=_model.Ad,
                    Soyadi=_model.Soyadi,
                    Turu=_model.Turu,
                    Eposta=_model.Eposta
                };
                db.AddObject("kisi", _insert);
                db.SaveChanges();
            }

        }

        public kisi Select(int KisiID)
        {
            var _select = db.kisi.FirstOrDefault(f => f.KisiID == KisiID);
            if (_select != null)
            {
                return _select;
            }
            else
            {
                return new kisi();
            }
        }

        public IEnumerable<kisi> SelectAll()
        {
            return db.kisi.OrderByDescending(o => o.KisiID).ToList();
        }

        public void Delete(int KisiID)
        {
            var _delete = db.kisi.FirstOrDefault(f => f.KisiID == KisiID);
            if (_delete != null)
            {
                db.DeleteObject(_delete);
                db.SaveChanges();
            }
        }

    }
}

I am getting errors. AddObject and DeleteObject codes are red underlined. And VS is talking about a reference. What am I missing ?

<updated code="">

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

namespace Panelium.Models
{
    public class KisiBLL
    {
        PaneliumEntities db = new PaneliumEntities();
        //crud
        public void InsertorUpdate(kisi _model)
        {
            if (_model.KisiID > 0)
            {
                // update
                var _update = db.kisi.FirstOrDefault(f => f.KisiID == _model.KisiID);
                if (_update != null)
                {
                    _update.Ad = _model.Ad;
                    _update.Soyadi = _model.Soyadi;
                    _update.Turu = _model.Turu;
                    _update.Eposta = _model.Eposta;
                    db.SaveChanges();
                }
            }
            else
            {
                // insert
                kisi _insert = new kisi
                {
                    Ad = _model.Ad,
                    Soyadi = _model.Soyadi,
                    Turu = _model.Turu,
                    Eposta = _model.Eposta
                };
                db.kisi.AddObject(_insert);
                db.SaveChanges();
            }

        }

        public kisi Select(int KisiID)
        {
            var _select = db.kisi.FirstOrDefault(f => f.KisiID == KisiID);
            if (_select != null)
            {
                return _select;
            }
            else
            {
                return new kisi();
            }
        }

        public IEnumerable<kisi> SelectAll()
        {
            return db.kisi.OrderByDescending(o => o.KisiID).ToList();
        }

        public void Delete(int KisiID)
        {
            var _delete = db.kisi.FirstOrDefault(f => f.KisiID == KisiID);
            if (_delete != null)
            {
              db.DeleteObject(_delete);
                db.SaveChanges();
            }
        }

    }
}
<pre lang="c#">
Posted
Updated 2-Oct-13 2:42am
v2
Comments
tanweer 2-Oct-13 7:22am    
Hi, "codes are red underlined" is not enough what we can give you a beter solution, please build the solution and get the exact error message and post here
FoxRoot 2-Oct-13 7:27am    
Sorry, my fault.

Error 1 'Panelium.Models.PaneliumEntities' does not contain a definition for 'AddObject' and no extension method 'AddObject' accepting a first argument of type 'Panelium.Models.PaneliumEntities' could be found (are you missing a using directive or an assembly reference?) E:\ZirvePaneliumProjesi\Panelium\Panelium\Models\KisiBLL.cs 20 Panelium

Error 2 'Panelium.Models.PaneliumEntities' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'Panelium.Models.PaneliumEntities' could be found (are you missing a using directive or an assembly reference?) E:\ZirvePaneliumProjesi\Panelium\Panelium\Models\KisiBLL.cs 18 Panelium

Check this sample application:

Entity Framework for Beginners[^]
 
Share this answer
 
v2
Change
db.AddObject("kisi", _insert);

into
db.kisi.AddObject(_insert)


and
db.DeleteObject(_delete);

into
db.kisi.DeleteObject(_delete);


The you should be fine...
 
Share this answer
 
Comments
FoxRoot 2-Oct-13 8:21am    
Thanks for your responce. However, stil same error.
Govindaraj Rangaraj 2-Oct-13 8:35am    
Could you please post your updated code? I reckon Eduard's suggestion, if implemented, should work fine.
FoxRoot 2-Oct-13 8:43am    
Posted the updated code.
Govindaraj Rangaraj 2-Oct-13 8:45am    
Could it be because you still did not change it for delete?
FoxRoot 2-Oct-13 8:47am    
I changed. Erorrs are still going on.

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