简体   繁体   English

不通过实体框架模型C#将数据添加到SQL数据库

[英]Not adding data to SQL database through Entity Framework model C#

This question might be asked but I could not find the answer for now so please help me with this. 可能会问这个问题,但我暂时找不到答案,请为此提供帮助。 I'm trying to add a new row into my SQL database through EF model. 我正在尝试通过EF模型将新行添加到SQL数据库中。 Is there anything wrong because it looks okay but actually does not insert anything to the table. 有什么问题吗,因为它看起来还不错,但实际上没有在表中插入任何内容。

private void Bsubmit_Click(object sender, EventArgs e)
{
        using(var db = new Models.CompanyDBEntities())
        {
            db.Clients.Add(new Client
            {
                name = nameTB.Text,
                age = Convert.ToInt32(ageTB.Text)
            });
            db.SaveChanges();
        }
}

EDIT** So now I'm adding a Change Tracker to see what's going on: 编辑**所以现在我要添加一个变更跟踪器以查看发生了什么:

        if (db.ChangeTracker.HasChanges()){
            db.SaveChanges();
            var clients = (from c in db.Clients select c).ToList();
            string updated = "";
            foreach (var c in clients)
            {
                updated += c.Name;
            }
            MessageBox.Show(updated);
        }

The message box does show up the name of current clients in the data together with the name I just added. 该消息框的确会在数据中显示当前客户端的名称以及我刚刚添加的名称。 I think the problem here is .SaveChanges() it's not working. 我认为这里的问题是.SaveChanges()无法正常工作。 I did not find the solution yet. 我尚未找到解决方案。

Try add Parameterless Constructor to your class and try again, just guess: 尝试将无参数构造函数添加到您的类中,然后重试,只需猜测:

public class Client
{
    public Client()
    {

    }

    public string name ...
    public int age ...
    ...
}

and use parentheses when new: 'new Client () {' 并在新的时候使用括号:'new Client () {'

        db.Clients.Add(new Client()
        {
            name = nameTB.Text,
            age = Convert.ToInt32(ageTB.Text)
        });

尝试运行Sql Server Profiler ,并检查针对您的语句执行的查询,很可能您会找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#实体框架在向数据库添加数据时未更新Wpf GUI - C# Entity Framework Not Updating Wpf GUI on Adding Data to Database C#实体框架,通过多对多连接将数据添加到数据库 - C# Entity Framework, adding data to database with Many-to-Many connections 通过C#中的OPC UA客户端插入具有ADO.NET实体数据模型的数据库 - Insert to Database with ADO.NET Entity Data Model through OPC UA Client in C# C# 实体框架核心写入原始 SQL 没有 model - C# Entity Framework Core write raw SQL without model C#读取大型Excel XLSX并将数据写入到SQL Server Express数据库时出现实体框架性能问题 - C# reading large excel xlsx and writing data to sql server express database with entity framework performance issues 使用C#将数据添加到SQL数据库无结果 - Adding data to SQL database with C# with no result C#将数据添加到SQL数据库 - C# adding data to SQL database 使用IValidatableObject将数据验证添加到WPF和C#中的实体框架实体 - Adding Data Validation with IValidatableObject to Entity Framework entities in WPF and C# 如何在C#Entity Framework模型中使用Json数据类型? - How to use Json data type in C# Entity Framework model? 在C#Entity-Framework中过滤对象模型的数据 - Filter Data of Object Model in C# Entity-Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM