簡體   English   中英

簡單的一對一關系實體框架代碼優先

[英]Simple one-to-one relationship Entity Framework code-first

我有兩個表,一個用於客戶,一個用於信用卡,並且它們之間存在簡單的一對一關系(這不是真正的數據,僅用於學習目的)。

即使數據庫中沒有錯誤和數據,我也無法為客戶顯示任何信用卡信息。

Customer.cs (型號):

public class Customer
{
    [Key]
    public int Id { set; get; }
    public string Username { set; get; }
    public string Password { set; get; }
    public string FirstName { set; get; }
    public string Surname { set; get; }
    public string Email { get; set; }
    public CreditCard CreditCard { get; set; }
}

Customer表:

客戶表

CreditCard.cs (型號):

public class CreditCard
{
    [Key, ForeignKey("Customer")]
    public int Id { get; set; }
    public string NameOnCard { get; set; }
    public string CardNumber { get; set; }
    public DateTime ExpiryDate { get; set; }
    public string CCV { get; set; }
    public Customer Customer { get; set; }
}

CreditCards表:

信用卡表

要顯示模型,我只需將客戶從DB傳遞到我的視圖:

控制器:

public class AuthenticationController : Controller
{
    private SchemaDBContext db = new SchemaDBContext();
    public ActionResult Login()
    {
        User user = new User();
        return View(user);
    }

    [HttpPost]
    public ActionResult Login(User user)
    {
        Customer customer = new Customer();
        if (ModelState.IsValid)
        {
            customer = db.Customers.FirstOrDefault(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password));
            if (customer!=null)
            {
                FormsAuthentication.SetAuthCookie(user.Username, false);
                return View("test", customer);
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
            }
        }
        return View(customer);
    }
}

視圖:

@using MVC_COMP1562.Models
@model Customer

@Html.DisplayFor(model => model.Username)
@Html.DisplayFor(model => model.Password)
@Html.DisplayFor(model => model.FirstName)
@Html.DisplayFor(model => model.Surname)
@Html.DisplayFor(model => model.Email)
@Html.DisplayFor(model => model.CreditCard.NameOnCard)
@Html.DisplayFor(model => model.CreditCard.CardNumber)
@Html.DisplayFor(model => model.CreditCard.CCV)

用於初始化測試數據的種子

 public class SchemaDBInitializer : DropCreateDatabaseAlways<SchemaDBContext>
    {
        protected override void Seed(SchemaDBContext context)
        {
            var customers = new List<Customer>
            {
                new Customer
                {
                    CreditCard = null,
                    Email = "carsonalexander@gmail.com",
                    Id = 1,
                    Password = "carsonPass",
                    Username = "carsonUser",
                    FirstName = "Alexander",
                    Surname = "Carson"
                },
                new Customer
                {
                    CreditCard = null,
                    Email = "alonsomeredith@gmail.com",
                    Id = 2,
                    Password = "alonsoPass",
                    Username = "alonsoUser",
                    FirstName = "Meredith",
                    Surname = "Alonso"
                }
            };

            customers.ForEach(s => context.Customers.Add(s));
            context.SaveChanges();

            var creditcards = new List<CreditCard>
            {
                new CreditCard
                {
                    CardNumber = "1234 3456 7890 1111",
                    CCV = "111",
                    Customer = customers[0],
                    ExpiryDate = new DateTime(2018, 04, 01),
                    Id = 1,
                    NameOnCard = "Alexander Carson"
                },
                new CreditCard
                {
                    CardNumber = "2222 0000 1234 6877",
                    CCV = "222",
                    Customer = customers[1],
                    ExpiryDate = new DateTime(2019, 05, 01),
                    Id = 2,
                    NameOnCard = "Meredith Alonso"
                }
             };

            creditcards.ForEach(s => context.CreditCards.Add(s));
            context.SaveChanges();

        }
    }

如果有其他人在解決我遇到的同樣問題。

感謝Gert Arnold我已經弄明白了,因為我實際上並沒有將任何數據分配給對象子對象,我認為它在Entity Framework中是自動的。

答案是延遲加載。

例如

每當我們在模型類中使用時,另一個模型作為關系將其聲明為虛擬,現在一切都會工作,否則當您從數據庫中獲取這些對象時,您必須手動加載這些對象的數據。

public class CreditCard
{
    [Key, ForeignKey("Customer")]
    public int Id { get; set; }
    public string NameOnCard { get; set; }
    public string CardNumber { get; set; }
    public DateTime ExpiryDate { get; set; }
    public string CCV { get; set; }
    public virtual Customer Customer { get; set; } //<--- Here virtual
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM