I recently tried to use Entity Framework in my WinForms project for accessing SQLServer database. There were some data already existing in the DB before integrating it in the solution.
I wrote some code to add a new item to the database. As follows
So what is the problem?
The problem is restoring the database file when building the project.
Is this really an issue? Will this affect my application when releasing?
Nope, this will not affect your application or database unless you rebuild it. You can simply verify this through following steps.
1. Run your application in visual studio.
2. Make changes in the database using your application.
3. Close the application.
4. Run the application without using visual studio. (Open Windows Explorer and navigate to the folder where your executable files reside. Run the executable by double clicking it).
5. Check whether the previous changes still exists. (It is there huh!)
I wrote some code to add a new item to the database. As follows
AccountantEntities db = new
AccountantEntities();
public bool CreateStock(long product,
DateTime created, double price, string serial, DateTime expire,
string desc)
{
try
{
Stock stock = new Stock();
stock.ProductId = product;
stock.CreatedOn = created;
stock.Description = desc;
stock.ExpireOn = expire;
stock.Price = price;
stock.SerialNo = serial;
stock.Status = Status.AVAILABLE;
db.Stocks.Add(stock);
db.SaveChanges();
return true;
}
catch { return
false; }
}
|
The
above code works fine for the moment. The problem happens when we start
debugging the project next time (ie, when we build the project). All
the changes made in the previous run is now cleared. Ie, the build
process restored my database file.I
didn’t notice this scenario for a long time and searched the internet
for a solution. But no actual solution found till now (Thanks to my
searching skills!).
So what is the problem?
The problem is restoring the database file when building the project.
Is this really an issue? Will this affect my application when releasing?
Nope, this will not affect your application or database unless you rebuild it. You can simply verify this through following steps.
1. Run your application in visual studio.
2. Make changes in the database using your application.
3. Close the application.
4. Run the application without using visual studio. (Open Windows Explorer and navigate to the folder where your executable files reside. Run the executable by double clicking it).
5. Check whether the previous changes still exists. (It is there huh!)
Is there any way I could avoid this issue?
Yep. Use a database that reside outside your solution folder.
Comments
Post a Comment