【发布时间】:2013-09-19 21:32:06
【问题描述】:
我正在尝试使用实体框架保存实体,尽管 EF 没有更新自动增量字段
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
}
我从 mySql 分析了以下命令:
INSERT INTO `address`(
`Id`,
`DatacenterId`,
)VALUES(
0,
1,
)
【问题讨论】:
标签: mysql entity-framework ef-code-first