实体框架不会更新mySql中计算密钥的自动增量字段 [英] Entity Framework does not update an auto-increment field of a computed key in mySql

查看:98
本文介绍了实体框架不会更新mySql中计算密钥的自动增量字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用实体框架保存实体,尽管EF不更新自动递增字段

  public类地址
{
public int Id {get;组; }
public int DatacenterId {get;组; }
public virtual Datacenter Datacenter {get;组; }
}

public class AddressMapping:EntityTypeConfiguration< Address>
{
public AddressMapping()
{
this.HasKey(k => new {k.Id,k.DatacenterId});
this.HasRequired(itr => itr.Datacenter)
.WithMany()
.HasForeignKey(fk => fk.DatacenterId);

this.Property(p => p.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}

使用(var context = new MyContext())
{
var address = new Address(); //初始化并设置所有值。包含FK
context.Address.Add(address);
context.SaveChanges();

//address.Id仍然是0
}

我从mySql中分析了以下命令:

  INSERT INTO'address`(
`Id`,
`DatacenterId`,
)VALUES(
0,
1,


解决方案

我已经改变了映射命令的顺序,它正在工作。我不知道EF映射命令可以改变映射结果。

  public class AddressMapping:EntityTypeConfiguration< Address> 
{
public AddressMapping()
{
this.Property(p => p.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

this.HasKey(k => new {k.Id,k.DatacenterId});

this.HasRequired(itr => itr.Datacenter)
.WithMany()
.HasForeignKey(fk => fk.DatacenterId);
}
}

最后它生成了正确的sql命令: (
`DatacenterId`,
)VALUES(
1,
);
SELECT
`Id`
FROM
`address`
WHERE
row_count()> 0 AND`Id` = last_insert_id()AND'DatacenterId` = 1


I'm trying to save an entity using entityframework, although the EF is not update the auto-increment field

public class Address
{
    public int Id { get; set; }
    public int DatacenterId { get; set; }
    public virtual Datacenter Datacenter { get; set; }
}

public class AddressMapping : EntityTypeConfiguration<Address>
{
    public AddressMapping()
    {
        this.HasKey(k => new { k.Id, k.DatacenterId });
        this.HasRequired(itr => itr.Datacenter)
            .WithMany()
            .HasForeignKey(fk => fk.DatacenterId);

        this.Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

using (var context = new MyContext())
{
    var address = new Address(); //Initialize and set all values. Include the FK
    context.Address.Add(address);
    context.SaveChanges();

    //address.Id is still 0
}

I profiled the following command from mySql:

INSERT INTO `address`(
    `Id`, 
    `DatacenterId`, 
)VALUES(
    0, 
    1, 
)

解决方案

I've changed the order of mapping commands and it is working. I didn't know the EF mapping commands could change the mapping result.

public class AddressMapping : EntityTypeConfiguration<Address>
{
    public AddressMapping()
    {
        this.Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.HasKey(k => new { k.Id, k.DatacenterId });

        this.HasRequired(itr => itr.Datacenter)
            .WithMany()
            .HasForeignKey(fk => fk.DatacenterId);
    }
}

And finally it generated the correct sql command:

INSERT INTO `address`(
    `DatacenterId`, 
)VALUES(
    1, 
);
SELECT
    `Id`
FROM 
    `address`
WHERE  
     row_count() > 0 AND `Id` = last_insert_id() AND `DatacenterId` = 1

这篇关于实体框架不会更新mySql中计算密钥的自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆