Nithin B
Nithin B

Reputation: 680

Why entity framework not inserting data to .mdf file?

I am using console application to insert data into .mdf file which is in App_Data folder and .edmx file in project folder.

The same below code is working with mvc but not console application.

Table is:

CREATE TABLE [dbo].[tbLegalDetails] (
[LegalStatusId]           INT            NOT NULL,
[LegalStatusName]         TEXT           NULL,
[LegalStatusHeading]      TEXT           NULL,
[PatentOfInvention]       NVARCHAR (100) NULL,
[EventCode]               VARCHAR (100)  NULL,
[EventExplination]        TEXT           NULL,
[CCOfCorrespondingPatent] NVARCHAR (100) NULL,
[CorrespondingPatentD]    INT            NULL,
PRIMARY KEY CLUSTERED ([LegalStatusId] ASC)
);

Code to insert data is:

ConsoleApplication__DynamicWebBrowserDatabaseEntities db = new ConsoleApplication__DynamicWebBrowserDatabaseEntities();
List<tbLegalDetail> hLegalDetaili = db.tbLegalDetails.ToList();

[Here count is shown as 0]

tbLegalDetail tbLegalDetail = new tbLegalDetail();
tbLegalDetail.LegalStatusId = 3;
tbLegalDetail.LegalStatusName = "INPADOC legal";
tbLegalDetail.LegalStatusHeading = "COMPOSITIONS ANDAPY";
tbLegalDetail.PatentOfInvention = "2003287377";
tbLegalDetail.EventCode = "FGA";
tbLegalDetail.EventExplination = "LETTSTANDARD PATENT";
tbLegalDetail.CCOfCorrespondingPatent = "dfs";
tbLegalDetail.CorrespondingPatentD = 1212;
db.tbLegalDetails.Add(tbLegalDetail);
db.SaveChanges();
List<tbLegalDetail> hLegalDetail = db.tbLegalDetails.ToList();

[Here count is shown as 1]

Console.ReadLine();

but when I look into the .mdf file I cant see the added row. When I run again I will show count as 0.

Please help.

Upvotes: 1

Views: 674

Answers (2)

Nithin B
Nithin B

Reputation: 680

I am answering this question form the comments given by @sgmoore and @vipin.

I changed the |DataDirectory| in the connection string in App.config file to full path which solved the problem

Thank you.

Upvotes: 1

vipin
vipin

Reputation: 367

Console application or windows form application uses resource from debug folder(A copy). May you got your changes from the .mdf file presented in your debug folder (Just after the run / Before the second run).

May this article talks you more.. http://www.codeproject.com/Articles/663453/Understanding-Clean-Build-and-Rebuild-in-Visual-St

Upvotes: 2

Related Questions