nanonerd
nanonerd

Reputation: 1984

entity framework insert not working

I learned some basic stuff on EF 4.0 and recently upgraded to EF 6.0. I can't seem to get a simple insert working. Has the "ent.AddtoImage()" been deprecated in version 6.0? I searched around but cannot find an answer.

using (evEntities ent = new evEntities())
{
Image insertImg = new Image();                                        

insertImg.TypeID = "a";
ent.AddtoImage(insertImg);
ent.SaveChanges();
}

I get a red squiggly line under AddtoImage ???

Edit: here is an image -

enter image description here

Upvotes: 1

Views: 4326

Answers (3)

ElectricRouge
ElectricRouge

Reputation: 1279

Try this instead

evEntities.Entry<Image>(ent).State = EntityState.Added;
ent.SaveChanges();

or

evEntities.Set<Image>().Add(ent);
ent.SaveChanges();

Upvotes: 0

Roland Kovacs
Roland Kovacs

Reputation: 26

Assuming that Image is an entity generated by EF you can use

ent.Images.Add(insertImg);

in EF6, then the rest of your code.

Upvotes: 1

TomTom
TomTom

Reputation: 62093

Yeah.

beacause evEntities is valid only in the scope of the using statement, which is the next block.

As you miss {} - that next blcock is the next statement, which is

Image insertImg = new Image ();

Right after that line, evEntities is disposed and the variable invalid.

Wrap the whole lines after using into curly braces to make them one block.

using (evEntities ent = new evEntities()) {
    ....rest of your code
}

Upvotes: 1

Related Questions