0

Having Trouble with Entity Framework. I have been adding a new row to Iktato table but is not work.

CREATE TABLE [Iktato] (
[TIPACT] nvarchar(3), 
[NRINREG] int NOT NULL identity(1,1), 
[DATAINREG] datetime, 
[NRACT] nvarchar(10), 
[DATAACT] datetime, 
[CODARHIVA] nvarchar(20), 
[NRFILE] int, 
[NRANEXE] int, 
[EMITENT] nvarchar(50), 
[TERMEN] datetime, 
[RESP1] nvarchar(50), 
[RESP2] nvarchar(50), 
[RESP3] nvarchar(50), 
[RESP4] nvarchar(50), 
[DESCRIERE] ntext, 
[PRIORITATE] nvarchar(13), 
[STATUT] nvarchar(20), 
[FILENAME] nvarchar(128),
PRIMARY KEY (NRINREG)
)
GO


CREATE TABLE [rasp] (
[TIPACT] nvarchar(3), 
[NRINREG] int NOT NULL, 
[DATAINREG] datetime, 
[NRACT] nvarchar(10), 
[DATAACT] datetime, 
[CODARHIVA] nvarchar(20), 
[NRFILE] int, 
[NRANEXE] int, 
[EMITENT] nvarchar(50), 
[TERMEN] datetime, 
[RESP1] nvarchar(50), 
[RESP2] nvarchar(50), 
[RESP3] nvarchar(50), 
[RESP4] nvarchar(50), 
[DESCRIERE] ntext, 
[PRIORITATE] nvarchar(13), 
[STATUT] nvarchar(20), 
[FILENAME] nvarchar(128),  
[ROW_ID] int NOT NULL IDENTITY,
PRIMARY KEY (ROW_ID)
)
GO


ALTER TABLE rasp 
ADD CONSTRAINT fk_Iktato 
FOREIGN KEY (NRINREG) 
REFERENCES Iktato (NRINREG) ON DELETE CASCADE 

GO

It`s a sequence of my program:

private RegistruEntities entities;
private Iktato proba;

proba = new Iktato { NRINREG = 5 };
entities.AttachTo("Iktatoes", proba);
entities.SaveChanges();

I want to insert a row with registration number 5 which will be saved in the database Iktato. Save it not work.

Please help !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
XYZ xyz
  • 1
  • 1
  • How does it *not work* ? Do you get an error - if so: **what** error? Also: can you show us your connection string used for this app? – marc_s Jul 01 '12 at 07:40
  • No errors but not inserted a new row in my Iktato table ! I use sqlce 3.5 . A delete row work ok. Here the code: private void delete_Click(object sender, RoutedEventArgs e) { if (iktatoesViewSource.View.CurrentPosition >= 0) { int wval1 = Convert.ToInt32(nRINREGLabel.Content); proba = entities.Iktatoes.Single(iktato => iktato.NRINREG == wval1); entities.DeleteObject(proba); entities.SaveChanges(); iktatoesViewSource.DeferRefresh(); } } – XYZ xyz Jul 01 '12 at 09:07
  • Please **do not** put code samples or sample data into comments - since you cannot format it, it's **extremely hard** to read it.... Instead: **update** your question by editing it to provide that additional information! Thank you. – marc_s Jul 01 '12 at 09:08
  • No errors but not inserted a new row in my Iktato table ! I use sqlce 3.5 . – XYZ xyz Jul 01 '12 at 09:17

2 Answers2

1

New entities have to be inserted using AddObject, not AttachTo:

proba = new Iktato { NRINREG = 5 };
entities.AddObject("Iktatoes", proba);
entities.SaveChanges();

You should also have an ObjectSet<Iktato> as a member of your ObjectContext that allows to use a more strongly typed version to avoid the string "Iktatoes" for the entity set:

proba = new Iktato { NRINREG = 5 };
entities.Iktatoes.AddObject(proba);
entities.SaveChanges();
Slauma
  • 175,098
  • 59
  • 401
  • 420
0

Try This. It Works for me

public ItemCategory Create(ItemCategory objItemCategory)
    {            
        context.ItemCategory.Add(objItemCategory);
        context.SaveChanges();

        return objItemCategory;
    }
Md. Nazrul Islam
  • 2,809
  • 26
  • 31