SQL Server和实体框架不保存更改

时间:2022-01-15 07:41:45

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.

我有以下问题:我创建了一个SQL Server LocalDB实例并创建了一个名为player的简单表。然后我添加了一个ADO.Net实体框架数据模型(数据库到代码)并实例化了POCO类Player。

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

然后我将实例添加到DbContext的Player属性中,然后执行了SaveChanges()方法 - 完全没有效果。

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

也许我采用了错误的SQL提供程序(因为有2个SQL Server提供程序:localdb和SQL Server)?

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

这是我的.mdf文件(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...

然后我添加了一个表并将主键设置为Identity ad NOT Null等...

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

我不知道为什么,但是我添加到contexts属性的实例都没有被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 SQL Server和实体框架不保存更改

编辑:多数民众赞成我如何创建一个新的localdb实例

Thats how I made a new table: SQL Server和实体框架不保存更改

多数民众赞成我如何制作一张新桌子:

Thats how I use the EntityFramework SQL Server和实体框架不保存更改

多数民众赞成我如何使用EntityFramework

1 个解决方案

#1


From my understanding you are trying to add an row, not a new table, I might be wrong, but you already define the tables ether code-first, model first or database-first.

根据我的理解,你试图添加一行,而不是新表,我可能是错的,但你已经定义了以太代码优先,先建立模型或先建立数据库。

using (var db = new PlayerContext()) 
{ 
    Player player= new Player() 
    { 
        IpAddress = "test";
    }; 
    var newRow = context.Set<Player>();
    newRow.Add(player); 
    db.SaveChanges(); 
}
//sidenote: once saveChanges() occured, Id of the new player is saved in the 'player' object

If you really trying to add tables while running the application (on user input), I recommend you re-think your database design (ERD). If you are trying to do code-first Initialization, then this and this will get you started.

如果您在运行应用程序时(在用户输入上)确实尝试添加表,我建议您重新考虑数据库设计(ERD)。如果您正在尝试执行代码优先初始化,那么这将使您开始。

#1


From my understanding you are trying to add an row, not a new table, I might be wrong, but you already define the tables ether code-first, model first or database-first.

根据我的理解,你试图添加一行,而不是新表,我可能是错的,但你已经定义了以太代码优先,先建立模型或先建立数据库。

using (var db = new PlayerContext()) 
{ 
    Player player= new Player() 
    { 
        IpAddress = "test";
    }; 
    var newRow = context.Set<Player>();
    newRow.Add(player); 
    db.SaveChanges(); 
}
//sidenote: once saveChanges() occured, Id of the new player is saved in the 'player' object

If you really trying to add tables while running the application (on user input), I recommend you re-think your database design (ERD). If you are trying to do code-first Initialization, then this and this will get you started.

如果您在运行应用程序时(在用户输入上)确实尝试添加表,我建议您重新考虑数据库设计(ERD)。如果您正在尝试执行代码优先初始化,那么这将使您开始。