samedi 9 mai 2015

SQL Server and Entity Framework don't save changes

I have the following issue: I created a SQL Server LocalDB instance and created a simple table called player. Then I added an ADO.Net Entity Framework Data Model (database to code) and instantiated the POCO class Player.

Then I added the instance to the DbContext's Player property and I executed the SaveChanges() method - with no effect at all.

Maybe I took the wrong SQL provider (cause there are 2 SQL Server providers: localdb and SQL Server)?

That are the properties of my .mdf file (the db):

Provider: .NET Framework-Datenanbieter für SQL Server
Type: Microsoft SQL-Server
Connection string: Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Me\documents\visual studio 2013\Projects\MsSQLSample\MsSQLSample\myLocalDb.mdf";Integrated Security=True

I created the database like this:

New Element --> Service-based Db.

Then I added a table and set the primary key as Identity ad NOT Null etc...

I don't know why but none of the instances I added to the contexts property was taken by the db.

using (var db = new PlayerContext()) 
{ 
    Table table = new Table() 
    { 
        IpAddress = "test";
    }; 

    db.Table.Add(table); 
    db.SaveChanges(); 
}

EDIT: Thats how I made a new localdb-instance record_new_db

Thats how I made a new table: record_new_table

Thats how I use the EntityFramework new_entity_model

Aucun commentaire:

Enregistrer un commentaire