在SaveChanges()调用实体框架之后,无法看到数据库中的更改

时间:2013-03-15 07:37:05

标签: entity-framework savechanges

我的表Device只有一列UID nvarchar(128),这是我的实体:

[Table( Name="Device" )]
public class Device
{
    [Key]
    public string UID { get; set; }
}

当我尝试插入实体并将更改提交到数据库时,一切正常。但是在数据库中没有添加新行。如果我尝试使用相同的UID重复此操作 - 我会感到厌倦

  

违反PRIMARY KEY约束'PK_dbo.Devices'。无法在对象'dbo.Devices'中插入重复键。重复键值为...

怎么了?

编辑:

这是我的背景:

public class DeviceContext :  BaseDbContext, IDbContext
{
    public DbSet<Entity.Device> Device { get; set; }

    public new IDbSet<T> Set<T>() where T : class
    {
        return base.Set<T>();
    }

    public int SaveChanges()
    {
        return base.SaveChanges();
    }

    public void Dispose()
    {
        base.Dispose();
    }
}

失败SaveChanges()方法。 BaseDbContext只是“连接字符串层”。

DeviceContext context = new DeviceContext();
context.Device.Add(new Device() { UID = id });
context.SaveChanges();

1 个答案:

答案 0 :(得分:1)

错误表示数据已保存。所以我认为你正在查看错误的数据库。验证DbContext使用的连接字符串。

顺便说一句,您可以通过在调试器中查看context.Database.Connection.ConnectionString属性来查看使用了哪个连接。