我有两个表,一个用于客户,一个用于信用卡,并且它们之间存在简单的一对一关系(这不是用于学习目的的真实数据)。
即使数据库中没有错误和数据,我也无法为客户显示任何信用卡信息。
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();
}
}
答案 0 :(得分:0)
如果还有其他人在解决我遇到的同样问题。
感谢Gert Arnold我已经弄明白了,因为我实际上并没有将任何数据分配给对象子对象,我认为它在Entity Framework中是自动的。
答案是延迟加载。
e.g。
每当我们在模型类中使用时,另一个模型作为关系将其声明为虚拟,现在一切都会工作,否则当您从数据库中获取这些对象时,必须手动加载这些对象的数据。
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
}