无法更新使用实体框架我的数据库中的行...? [英] Can't update rows in my database using Entity Framework...?
问题描述
好了,这实在是不可思议。我做了一个简单的数据库有一个表,客户,其中有一列,名称。从数据库I自动生成一个ADO.NET实体数据模型,和我想要一个新客户添加到它,像这样:
Okay, this is really weird. I made a simple database with a single table, Customer, which has a single column, Name. From the database I auto-generated an ADO.NET Entity Data Model, and I'm trying to add a new Customer to it like so:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main()
{
Database1Entities db = new Database1Entities();
Customer c = new Customer();
c.Name = "Harry";
db.AddToCustomer(c);
db.SaveChanges();
}
}
}
不过,这并不坚持客户哈利到数据库中!我一直在抓我的头,而现在不知道为什么这样一个简单的操作不起作用。究竟什么可能是问题!?
But it doesn't persist Customer "Harry" to the database! I've been scratching my head for a while now wondering why such a simple operation doesn't work. What on earth could be the problem!?
推荐答案
EF要求你有许多操作的唯一索引。
EF requires that you have a unique index for many operations.
尝试添加标识字段(主键)到表。删除并重新创建你的模型,然后重试。
Try adding an identity field (primary key) to your table. Delete and recreate your model and try again.
编辑:
看起来你是从一个控制台应用程序运行此。
It looks like you are running this from a console app.
- 请您在app.config文件中的连接字符串?
- 请您在您的解决方案多个项目?
- 您是否获得任何例外?
EDIT2:
接下来的事情尝试:
- 在使用SQL Server Profiler来看看什么是被发送到数据库
- 打开EF模型在编辑器中看到XML,检查是否有任何错误
这篇关于无法更新使用实体框架我的数据库中的行...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!