实体框架不更新mySql中计算密钥的自动增量字段
我试图使用entityframework保存一个实体,尽管EF没有更新自动增量字段实体框架不更新mySql中计算密钥的自动增量字段,mysql,entity-framework,ef-code-first,Mysql,Entity Framework,Ef Code First,我试图使用entityframework保存一个实体,尽管EF没有更新自动增量字段 public class Address { public int Id { get; set; } public int DatacenterId { get; set; } public virtual Datacenter Datacenter { get; set; } } public class AddressMapping : EntityTypeConfiguration
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
}
我已经更改了映射命令的顺序,它正在工作。我不知道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);
}
}
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);
}
}
INSERT INTO `address`(
`DatacenterId`,
)VALUES(
1,
);
SELECT
`Id`
FROM
`address`
WHERE
row_count() > 0 AND `Id` = last_insert_id() AND `DatacenterId` = 1