简体   繁体   中英

Failing to write to database using entity framework ( edmx )

As the title says, I have a problem writing to local database. I generated a edmx Model from this, and I can easily read from it.

EDMXNS.TOWDataBasev1Entities db = new EDMXNS.TOWDataBasev1Entities();

var query = from p in db.Accounts select p;

foreach (EDMXNS.Accounts s in query)
    Console.WriteLine(s.AccountName);

That works fine. However when I try to write to the database, nothing happens. I do not get any errors, exceptions etc. I figure, since I can read from the database, that it's not a connection problem.

Here is the code i have for writing.

EDMXNS.TOWDataBasev1Entities db = new EDMXNS.TOWDataBasev1Entities();
EDMXNS.Accounts acc = new EDMXNS.Accounts();

acc.AccountID = 1;
acc.AccountName = "testuser";
acc.AccountPW = "testpw";
acc.PersonDataID = 0;

db.AddToAccounts(acc);
db.SaveChanges();

It is worthwhile to meantion that my Accounts.AccountID has identity/autoincrement, but I have tried both setting it to the next known value, or simply not setting it at all.

Do anyone have an idea as to what might cause this problem?

EDIT: I also tried to remove the custom name space, delete all records of the database and reimport it all.

Removing the custom tool name space, results in errors like these: Ambiguity between 'TOWServer.Accounts.AccountName' and 'TOWServer.Accounts.AccountName'

Which doesnt tell me anything.

Reimporting everything now gives me an exception: "Unable to load the specified metadata resource"

I've always use this format when adding records with EF, Try following this format:

using (MovieStoreEntities context = new MoveStoreEntities()) 
    { 
      try 
      { 

        context.Movies.AddObject(new Movie() { MovieID = 234,  
             Title = "Sleepless Nights in Seattle", Quantity = 10 }); 
        context.SaveChanges(); 
      } 
      catch(Exception ex)  
      { 
        Console.WriteLine(ex.InnerException.Message);  
      } 
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM