简体   繁体   中英

Entity Framework Code First SaveChanges creates a new object

I am using the Code First approach with my project.

Here is my object:

public class Account
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public string name { get;set; }
}

If I create a new record it is OK:

using(var context = new context())
{
    context.Accounts.add(account);
    context.savechanges(); //This saves fine 
}

But when I change a property it saves another record in the database:

using (var context = new context())
{
    var account = context.Account.where(x => x.ID == GUID).FirstOrDefault();
    if (account != null)
    {
        account.name = "UpdatedName";
        context.savechanges(); // This creates a new record!!
    }
}

I am fairly new to Entity framework; why is it creating new records each time? Is it the attribute on the ID? If it is, then how can I use GUIDS as IDs instead of integers?

The attribute in your Account option should work fine to set up the ID column as the primary key for your objects.

If you are getting a new entry added to the database when you save changes, it is almost certainly the result of you having changed the primary key (ID property) of the object after having received it from the DB. Maybe you are trying to set the GUID property yourself in some piece of code that you haven't included? (You should be letting the DB assign it).

In any case, this simple console app uses your setup and works as expected. If you don't see an obvious place in your code where you are changing the GUID, maybe you can post your actual code? (I notice a couple of typos in what is pasted in, so it doesn't appear to be what you are actually using)

public class Account
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public string name { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Account> Accounts { get; set; }
}

class Program
{
    static Guid MyGuid = Guid.Empty;

    static void Main(string[] args)
    {
        using (var context = new MyContext())
        {
            Account account = new Account { name = "OldName" };
            context.Accounts.Add( account );
            context.SaveChanges();

            MyGuid = account.ID;
        }

        using (var context = new MyContext())
        {
            var account = context.Accounts.Where(x => x.ID == MyGuid).FirstOrDefault();
            if (account != null)
            {
                account.name = "UpdatedName";
                context.SaveChanges();
            }
        }
    }
}

I am using the Code First approach with my project.

Here is my object:

public class Account
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public string name { get;set; }
}

If I create a new record it is OK:

using(var context = new context())
{
    context.Accounts.add(account);
    context.savechanges(); //This saves fine 
}

But when I change a property it saves another record in the database:

using (var context = new context())
{
    var account = context.Account.where(x => x.ID == GUID).FirstOrDefault();
    if (account != null)
    {
        account.name = "UpdatedName";
        context.savechanges(); // This creates a new record!!
    }
}

I am fairly new to Entity framework; why is it creating new records each time? Is it the attribute on the ID? If it is, then how can I use GUIDS as IDs instead of integers?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM