-1

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=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AccountsDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=E:\Porjects\DB\EFtesting\WindowsFormsApplication3\WindowsFormsApplication3\AccountsDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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!

alentor
  • 27
  • 3
  • 12
  • Please read [ask] and include all relevant information in your question. Show your connection string, mention what build action and "copy to output" settings are active for the .mdf file, show how you inspect the records that are in the database and triple-check that you're actually looking in the file that your application is using. – CodeCaster Feb 04 '16 at 15:53

1 Answers1

0

I bet your problem is that you are not pointing to the right database. Depending on how you write your code it's possible that your code createas a new database in localDB and oes the operation in that DB.

Please, make sure that you have configured and are using the right database connection when you instance your DbContext. take into account that it depends on the configuration on your app/web.config connection strings and EF configuration, as well as the parameterspassed to your DbContext constructor, if any.

Please read the DbContext constructor documentation for further information. And also this one: Connections and Models

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • _"I bet"_ - wouldn't you rather post a comment to verify this, then? – CodeCaster Feb 04 '16 at 15:40
  • @Jonathan that's fine, but _if_ that is the actual problem, there's dozens of duplicates out there. – CodeCaster Feb 04 '16 at 15:44
  • @CodeCaster I thought of it at first, but as I'm 99% sure that his is the problem, I didn't. As this happens on many occasion, even if this is not the right answer for this case, I'm sure that it will be the right answer for may other people finding this same problem. Thanks for the advice. – JotaBe Feb 04 '16 at 15:44
  • @CodeCaster If there are duplicate over there, please mark the question as duplicated, and I'll follow you. – JotaBe Feb 04 '16 at 15:45
  • [C# mdf database doesn't update on program startup](http://stackoverflow.com/questions/15717655/c-sharp-mdf-database-doesnt-update-on-program-startup), [SQL insert query in C# does not update database](http://stackoverflow.com/questions/11228977/sql-insert-query-in-c-sharp-does-not-update-database), [Database isn't updating](http://stackoverflow.com/questions/5215791/database-isnt-updating) for example. I cannot mark this question as a duplicate, because there's not enough information to troubleshoot. – CodeCaster Feb 04 '16 at 15:47
  • As I said, I can fetch information, but not add/edit or remove, I'm pointing to the right database.. – alentor Feb 04 '16 at 15:51
  • How do you check if the entity has been added/removed? From Entity Framework? This maybe not your case but this error is common where the database get copied and the user check in the original database instead of the copied database. Fetch look to work but not "add"/"edit" where in fact it work. – Jonathan Magnan Feb 04 '16 at 16:09