简体   繁体   English

Entity Framework Code First SaveChanges 创建一个新对象

[英]Entity Framework Code First SaveChanges creates a new object

I am using the Code First approach with my project.我在我的项目中使用 Code First 方法。

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?是ID上的属性吗? If it is, then how can I use GUIDS as IDs instead of integers?如果是,那么我如何使用 GUIDS 作为 ID 而不是整数?

The attribute in your Account option should work fine to set up the ID column as the primary key for your objects.您帐户选项中的属性应该可以很好地将 ID 列设置为对象的主键。

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.如果您在保存更改时将新条目添加到数据库中,则几乎可以肯定是您在从数据库接收对象后更改了对象的主键(ID 属性)的结果。 Maybe you are trying to set the GUID property yourself in some piece of code that you haven't included?也许您正在尝试在一些您没有包含的代码中自己设置 GUID 属性? (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?如果您在代码中没有看到更改 GUID 的明显位置,也许您可​​以发布您的实际代码? (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?它是ID上的属性吗? If it is, then how can I use GUIDS as IDs instead of integers?如果是这样,那我怎么用GUIDS作为ID而不是整数呢?

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

相关问题 实体框架(代码优先)创建不必要的重复外键 - Entity Framework (code first) creates unecessary duplicated foreign keys 实体框架附加对象但不调用SaveChanges - Entity Framework Attaching an object but not calling SaveChanges 插入后Db.SaveChanges未分配主键ID - 代码优先实体框架 - Db.SaveChanges Not Assigning Primary Key ID After Insert - Code First Entity Framework 覆盖Entity Framework 5 Code Code中的SaveChanges以复制旧旧库的行为 - Overriding SaveChanges in Entity Framework 5 Code First to replicate the behavior of an old legacy library 保存更改后,如何首先使用实体​​框架代码从数据库中检索对象集合的ID - How to retrieve Ids of a collection of objects after savechanges from database using Entity Framework code first 如果子外键已经存在,则SaveChanges上的DbUpdateException-在Entity Framework代码中优先 - DbUpdateException on SaveChanges if child foreign key already exists - in Entity Framework Code First 代码优先实体框架添加对象 - Code First Entity Framework Adding Object object 实例化上的实体框架代码优先回调 - Entity Framework Code First callback on object instantiation 实体框架代码首先用于具有属性集合的对象 - Entity Framework code first for object with collection of properties 首先在实体框架代码中初始化数据库中的对象 - Initialize object in database in Entity Framework Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM