public class Card : EntityBase
{
[Required]
[MaxLength(16)]
[RegularExpression("([0-9]+)", ErrorMessage = "Only number allowed")]
public string Number { get; set; }
public bool IsActive { get; set; }
public Customer Customer { get; set; }
}
public class Customer : EntityBase
{
public Customer()
{
Address = new Address();
}
[Required]
[MaxLength(50)]
public string Name { get; set; }
public string PhoneNumber { get; set; }
public Address Address { get; set; }
[ForeignKey("Card")]
public int Card_Id { get; set; }
public Card Card { get; set; }
}
public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
{
HasRequired(x => x.Card).WithMany().HasForeignKey(x => x.Card_Id);
}
}
static void CreateCustomer()
{
using (var context = new BenzineFleetContext())
{
var card = var card = new Card
{
Number = "123456"
};
context.Cards.Add(card);
context.SaveChanges();
}
using (var context = new BenzineFleetContext())
{
var card = context.Cards.First(c => c.Number == "123456");
var customer = new Customer { Name = "Rayz", Card = card };
context.Customers.Add(customer);
context.SaveChanges();
}
}
新しい Customer を作成し、データベースからカードを使用したいのですが、データベースに保存すると、Customer テーブルの Card_Id は存在しますが、Card テーブルの Customer_Id は null です。テーブルカードで Customer_Id を更新するには?
私の英語が下手で申し訳ありません:(助けてくれてありがとう:)