Question

I have a table Device with only one column UID nvarchar(128) and here is my entity:

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

When I trying to insert entity and commit changes to database all is ok. But in database there are no new rows added. If I try to repeat this operation with the same UID - I get en eror

Violation of PRIMARY KEY constraint 'PK_dbo.Devices'. Cannot insert duplicate key in object 'dbo.Devices'. The duplicate key value is ...

What's wrong?

EDIT:

Here is my context:

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();
    }
}

Fails SaveChanges() method. BaseDbContext is only "connectionstring layer".

DeviceContext context = new DeviceContext();
context.Device.Add(new Device() { UID = id });
context.SaveChanges();
Was it helpful?

Solution

Error says that data is already saved. So I think you are looking at wrong database. Verify connection string which is used by your DbContext.

BTW you can see which connection is used by watching at context.Database.Connection.ConnectionString property in debugger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top