Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC-实体框架-这属于哪里?_C#_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

C# MVC-实体框架-这属于哪里?

C# MVC-实体框架-这属于哪里?,c#,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,我有以下简化课程: public class Customer { public int Id {get; set;} public string FirstName {get; set;} public string LastName {get; set;} public List<Account> Accounts {get; set;} } public class Account { public int Id {get; set;}

我有以下简化课程:

public class Customer
{
   public int Id {get; set;}
   public string FirstName {get; set;}
   public string LastName {get; set;}
   public List<Account> Accounts {get; set;}
}

public class Account
{ 
   public int Id {get; set;}
   public Customer Customer {get; set;}
   public int CustomerId {get; set;}
   public string AccountNumber {get; set;}
   public double AccountBalance {get; set;}
   public int? SupercededAccountId {get; set;}
}

我不仅需要在Account视图中访问被替换的Account对象,还需要访问报表和后台任务,因此我希望它以某种方式成为模型类的一部分。获取我所需的被替代帐户值的正确方法是什么?

创建被替代帐户的导航器

public class Account
{ 
   [Key]
   public int Id {get; set;}

   [ForgienKey("Customer")]
   public int CustomerId {get;set;}
   //navigator to customer
   public virtual Customer Customer {get; set;}
   public string AccountNumber {get; set;}
   public double AccountBalance {get; set;}

   [ForgienKey("SupercededAccount")]
   public int? SupercededAccountId {get; set;}

   public virtual Account SupercededAccount {get;set;}
}

public class Customer
{
   [Key]
   public int Id {get; set;}
   public string FirstName {get; set;}
   public string LastName {get; set;}

   public virtual ICollection<Account> Accounts {get; set;}
}
公共类帐户
{ 
[关键]
公共int Id{get;set;}
[ForgienKey(“客户”)]
public int CustomerId{get;set;}
//导航到客户
公共虚拟客户客户{get;set;}
公共字符串AccountNumber{get;set;}
公共双帐户余额{get;set;}
[ForgienKey(“替代数据计数”)]
public int?替换的daccountid{get;set;}
公共虚拟帐户取代了daccount{get;set;}
}
公共类客户
{
[关键]
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共虚拟ICollection帐户{get;set;}
}

Ok完美。更进一步。我还需要访问此客户以前的所有帐户。我在上面省略了它,但我也有一个方法来检索这些,基本上与上面相同,只是它返回一个帐户列表,其中CustomerId=Account.CustomerId和!=当前ID已更新,只需使用accounts navigator和您想要的linq。我可以将其放入方法中以便于访问吗?所以我可以在Account类中有一个List PreviousAccounts()方法,返回Customer.Accounts.Where(I=>I.Id!=this.Id)?你可以,但我更喜欢保持实体类精简,这意味着linq非常简单,通过直接访问方法,你不会真正节省很多。如果确实如此,请确保在查询之前检查集合是否为null。
public class Account
{ 
   [Key]
   public int Id {get; set;}

   [ForgienKey("Customer")]
   public int CustomerId {get;set;}
   //navigator to customer
   public virtual Customer Customer {get; set;}
   public string AccountNumber {get; set;}
   public double AccountBalance {get; set;}

   [ForgienKey("SupercededAccount")]
   public int? SupercededAccountId {get; set;}

   public virtual Account SupercededAccount {get;set;}
}

public class Customer
{
   [Key]
   public int Id {get; set;}
   public string FirstName {get; set;}
   public string LastName {get; set;}

   public virtual ICollection<Account> Accounts {get; set;}
}