当我要设置我的ID密钥时,在创建表时遇到问题,该表按顺序分配了其代码,我需要将其格式设置为“ 20190123”,以便以后进行搜索。
如何在表格中设置唯一键?
是否有必要为此删除自动增量?
public class RentalDbInitializer: DropCreateDatabaseAlways<RentalContext>
{
protected override void Seed(RentalContext context)
{
context.People.Add(new Person { Id = 1, FirstName = "Ivan", LastName = "Ivanov" });
context.RentalDates.Add(new RentalDate { Id = int.Parse(DateTime.Now.AddDays(2).ToString("yyyyMMdd", CultureInfo.InvariantCulture)), DateTimeRental = DateTime.Now.AddDays(2) });
context.Rentals.Add(new Rental { PersonId = 1, RentalDateId = int.Parse(DateTime.Now.AddDays(2).ToString("yyyyMMdd", CultureInfo.InvariantCulture)), Hours = 10, Cost = 200 });
base.Seed(context);
}
}