the behavior I'm dealing with is kinda weird, I can fetch information from the Database but not add/edit or remove. I'm not receiving any exception. it just won't update my Database : /
Any suggestions? I have tried using ID without increment, but I'm getting the same result.
I have even tried making a phone new project with a simple 2 column database but it still won't update, any suggestions on what I might be doing wrong?
Thank you!
Database1.mdf => accounts
CREATE TABLE [dbo].[Accounts] (
[AccountID] INT IDENTITY (1, 1) NOT NULL,
[LastName] VARCHAR (30) NULL,
[FirstName] VARCHAR (30) NULL,
[Password] VARCHAR (30) NULL,
[Email] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([AccountID] ASC)
);
Form1.cs
using System;
using System.Data.Entity;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication2 {
public partial class Form1: Form {
public Database1Entities AccountsContext = new Database1Entities();
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
using (Database1Entities context = new Database1Entities()) {
Account newAccount = new Account
{AccountID = 5, Email = "a", FirstName = "a", LastName = "a", Password = "a"};
context.Accounts.Add(newAccount);
context.SaveChanges();
}
using (var context = new Database1Entities()) {
Account newAccount = new Account
{Email = "b", FirstName = "b", LastName = "b", Password = "b", AccountID = 6};
context.Entry(newAccount).State = EntityState.Added;
context.SaveChanges();
}
AccountsContext.Accounts.Add(new Account
{Email = "c", FirstName = "c", LastName = "c", Password = "c", AccountID = 7});
//Account newAccount = new Account { Email = "a", FirstName = "a", LastName = "a", Password = "a"};
//DB.Accounts.Attach(newAccount);
AccountsContext.SaveChanges();
string str = null;
foreach (Account account in AccountsContext.Accounts.ToList()) {
str = str + account.AccountID;
}
label1.Text = str;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
AccountsContext.SaveChanges();
}
}
}
EDIT:
Old connection string =>
<add name="AccountsDBEntities" connectionString="metadata=res://*/model1Accounts1.csdl|res://*/model1Accounts1.ssdl|res://*/model1Accounts1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AccountsDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
New Connection string =>
<add name="AccountsDBEntities" connectionString="metadata=res://*/model1Accounts1.csdl|res://*/model1Accounts1.ssdl|res://*/model1Accounts1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=E:\Porjects\DB\EFtesting\WindowsFormsApplication3\WindowsFormsApplication3\AccountsDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I don't understand why |DataDirectory| was causing this weird behavior? shouldn't it map the directory to the projects root directory?
Thank you everyone!