首先,这是我的课程:
ie
理想地,当访问Person类时,我希望能够调用一个函数(或属性),该函数可以从PersonName存储库中提取该人的全名。但是,我不太确定该怎么做。有关这些表中数据的示例:
public class Name
{
public int NameId {get;set;}
public string Value {get;set;}
[ForeignKey("Name_NameId")]
public ICollection<PersonName> PersonsName {get;set;}
}
public class NameType
{
public int NameTypeId {get;set;}
public string Value {get;set;}
[ForeignKey("NameType_NameTypeId")]
public ICollection<PersonName> PersonsName {get;set;}
}
public class Person
{
public int PersonId {get;set;}
public string Suffix {get;set;}
public datetime DateOfBirth {get;set;}
[ForeignKey("Person_PersonId")]
public ICollection<PersonName> PersonsName {get;set;}
}
public class PersonName
{
public int Person_PersonId {get;set;}
public int Name_NameId {get;set;}
public int NameType_NameTypeId {get;set;}
public int Order {get;set;}
}
因此,在Person类中,我想要一个类似GetFullName()/ FullName的函数/属性,该函数/属性将返回“ John Jacomb Jingleheimer Schmidt”。我一直在从头开始研究一个教程,在制作完每个类之后,他们接着创建一个接口,mockrepository并在db存储库上工作。但是,我想通过视图模型将信息传递到视图中,但是我不确定如何将类绑定到存储库等。是否有人可以将我指向可以解释这一点的教程?最好还是为我拼出来?谢谢。
答案 0 :(得分:2)
我认为您的表格太复杂了。缺点是您必须联接许多表才能获得某人的全名...并且由于所有联接,您可能会遇到性能问题...其他开发人员很难理解和维护此代码。
所以,我知道我不是直接回答您的问题,但是我建议您简化表格设计。
为什么不使用简单的表,例如:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set;}
public string MiddleName { get; set;}
public string LastName { get; set; }
public string NickName { get; set; }
public string Suffix { get; set; }
public datetime DateOfBirth { get; set; }
// it is a lot easier to implement GetFullName() method here than joining so many tables
public string GetFullName()
{
retun FirstName + " " + MidleName + " " + LastName;
}
}
我真的很难理解您的上一张桌子:PersonName
public class PersonName
{
public int Person_PersonId { get; set; }
public int Name_NameId { get; set; }
public int NameType_NameTypeId { get; set; }
public int Order { get; set; } // <-- Why putting Order in PersonName?
}
对我来说,Order
不是PersonName表中的逻辑列...我不完全了解您的设计,我可能完全错了...但是对我来说,这感觉像是过度设计设计。
答案 1 :(得分:0)
希望我能正确理解您的问题
虽然我做了一些假设
我使用您的类来构建数据库并查询该数据库,以便其他表通过关系进行链接
假设您要传递人的ID,并且还假定dbcontext可用并在类内部实例化,则调用GetFullName
public string GetPersonById(int personId)
{
var person = db_context.PersonNames.FirstOrDefault(m => m.Person_PersonId == personId);
//did not check for null here
var name = person.Person.PersonNames.ToList();
var fullname = "";
name.ForEach(m => fulname += m.Name.Value + " ");
return fullname;
}
public virtual string GetFullName
{
get { return GetPersonById(PersonId); }// the person id passed, the class if instantiated can call this instance PersonId
set { ; }
}
此外,您还可以将数据从数据库获取到您拥有的模型中,并且该方法可以正常工作。从数据库中传递它们给您提供了联系能力,并且Razor Engine
在视图中可能会很有帮助,但是
要获取数据,您需要一个接一个地提取数据并将其存储在列表中。
public void UseModel()
{
var people = _context.People.ToList();
var names = _context.Names.ToList();
var name_types = _context.NameTypes.ToList();
List<PersonModel> ps = new List<PersonModel>();
people.ForEach(m =>
{
ps.Add(new PersonModel
{
PersonId = m.PersonId,
Suffix = m.Suffix,
DateOfBirth = m.DateOfBirth,
PersonsName = null//I suggest you use mapping here see https://stackoverflow.com/questions/16118085/best-practices-for-mapping-one-object-to-another
//use mapping to map the list or use a fooloop to construct it before this declaration and assign it here
});
});
//do for names and name_types
}
您还可以将此数据作为具有全部
的对象View
传递到PersonName
答案 2 :(得分:0)
映射您的视图模型。您可以这样使用。
vw_winning_report
映射FirstName,MiddleName,LastName,NickName。 您可以使用。
vw_winning_report