Asp.net mvc 4 模型中的MVC4列表

Asp.net mvc 4 模型中的MVC4列表,asp.net-mvc-4,Asp.net Mvc 4,我是ASP.NETMVC的新手,有一个包含联系人信息的模型,还有一个联系人注释列表。模型如下所示: public class Investor { public int Id { get; set; } public string Name { get; set; } public string Company { get; set; } public string Email { get; set; } public string Phone { get

我是ASP.NETMVC的新手,有一个包含联系人信息的模型,还有一个联系人注释列表。模型如下所示:

public class Investor
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Cell { get; set; }
    public string Fax { get; set; }
    [Display(Name="Address 1")]
    public string Address1 { get; set; }
    [Display(Name = "Address 2")]
    public string Address2 { get; set; }
    public string City { get; set; }
    [StringLength(2, ErrorMessage = "State must be 2 characters")]
    public string State { get; set; }
    public string Zip { get; set; }
    public List<Note> Notes { get; set; }
}
public class Note
{
    [Key]
    //[Column(Order = 0)]
    public string ContactTableId { get; set; }
    //[Key]
    //[Column(Order = 1)]
    public int? ContactId { get; set; }
    public string note { get; set; }
    public DateTime? DateCreated { get; set; }
}
    public ActionResult Edit(int id = 0)
    {

        string query = "SELECT * FROM Notes " +
            "WHERE ContactTableId = 'Investors' AND ContactId = " + id +
            " ORDER BY DateCreated DESC";
        var notes = db.Database.SqlQuery<Note>(query).ToList();

        Investor investor = db.Investors.Find(id);
        investor.Notes = notes;

        if (investor == null)
        {
            return HttpNotFound();
        }
        return View(investor);
    }
公共类投资者
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串公司{get;set;}
公共字符串电子邮件{get;set;}
公用字符串电话{get;set;}
公共字符串单元格{get;set;}
公共字符串传真{get;set;}
[显示(名称=“地址1”)]
公共字符串地址1{get;set;}
[显示(名称=“地址2”)]
公共字符串地址2{get;set;}
公共字符串City{get;set;}
[StringLength(2,ErrorMessage=“状态必须为2个字符”)]
公共字符串状态{get;set;}
公共字符串Zip{get;set;}
公共列表注释{get;set;}
}
公开课堂讲稿
{
[关键]
//[第列(顺序=0)]
公共字符串ContactTableId{get;set;}
//[关键]
//[第列(顺序=1)]
public int?ContactId{get;set;}
公共字符串注释{get;set;}
公共日期时间?DateCreated{get;set;}
}
在控制器中,我将票据加载到投资者对象中,如下所示:

public class Investor
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Cell { get; set; }
    public string Fax { get; set; }
    [Display(Name="Address 1")]
    public string Address1 { get; set; }
    [Display(Name = "Address 2")]
    public string Address2 { get; set; }
    public string City { get; set; }
    [StringLength(2, ErrorMessage = "State must be 2 characters")]
    public string State { get; set; }
    public string Zip { get; set; }
    public List<Note> Notes { get; set; }
}
public class Note
{
    [Key]
    //[Column(Order = 0)]
    public string ContactTableId { get; set; }
    //[Key]
    //[Column(Order = 1)]
    public int? ContactId { get; set; }
    public string note { get; set; }
    public DateTime? DateCreated { get; set; }
}
    public ActionResult Edit(int id = 0)
    {

        string query = "SELECT * FROM Notes " +
            "WHERE ContactTableId = 'Investors' AND ContactId = " + id +
            " ORDER BY DateCreated DESC";
        var notes = db.Database.SqlQuery<Note>(query).ToList();

        Investor investor = db.Investors.Find(id);
        investor.Notes = notes;

        if (investor == null)
        {
            return HttpNotFound();
        }
        return View(investor);
    }
公共操作结果编辑(int-id=0) { string query=“从注释中选择*”+ “其中ContactTableId=‘投资者’和ContactId=“+id+ “按日期创建的订单描述”; var notes=db.Database.SqlQuery(query.ToList(); 投资者=db.Investors.Find(id); 投资者。票据=票据; 如果(投资者==null) { 返回HttpNotFound(); } 返回视图(投资者); }
虽然这样做有效,但我认为有一种更优雅的“最佳实践”方法,可以使用外键或模型中的某种机制自动将Notes表加载到投资者手中,而无需在控制器中执行。有什么想法吗?

如我所见,您正在使用实体框架

string query = "SELECT * FROM Notes " +
    "WHERE ContactTableId = 'Investors' AND ContactId = " + id +
    " ORDER BY DateCreated DESC";
var notes = db.Database.SqlQuery<Note>(query).ToList();
此代码永远不会返回HttpNotFound:

if (investor == null)
{
    return HttpNotFound();
}
因为您将在此处获得
NullReferenceException
异常:

investor.Notes = notes;

如果您创建具有关系的表,EF可以自动加载
投资者的注释

实际上,我在LINQ方面遇到了问题。它应该可以工作,但我得到了一个错误“无效的列名'Investor_Id'。”这对我来说没有意义。我可以将语句简化为:var notes=db.notes.ToList();但仍然会出现相同的错误。我会添加注释类或注释表中没有“Investor\u Id”。@BlooSki有命名约定,可能EF试图查找PK,请尝试添加
[Key]
属性。