2015-09-28 72 views
2

我在我的控制器下面的方法:参考一个从另一个数据库asp.net mvc的另一个模型模型

public ActionResult OwnerList() 
    {        
     var owners = (from s in db.Owners      
        orderby s.Post.PostName 
        select s).ToList(); 

     var viewModel = owners.Select(t => new OwnerListViewModel 
     { 
      Created = t.Created, 
      PostName = t.Post.PostName, 
      Dormant = t.Dormant, 
      OwnerId = t.OwnerId, 
     }); 
     return PartialView("_OwnerList", viewModel); 
    } 

运行此我得到了业主的变量以下错误:

{ “无效的对象名称dbo.Post'。”}

我的模型是这些

在业主数据库

 public class Owner 
{ 
    public int OwnerId { get; set; } 

    [Column(TypeName = "int")] 
    public int PostId { get; set; } 

    [Column(TypeName = "bit")] 
    public bool Dormant { get; set; } 

    [Column(TypeName = "datetime2")] 
    public DateTime Created { get; set; } 

    public virtual ICollection<Asset> Assets { get; set; } 

    public virtual Post Post { get; set; } 
} 

在人物数据库

public class Post 
{ 
    public int PostId { get; set; } 

    [StringLength(50)] 
    [Column(TypeName = "nvarchar")] 
    public string PostName { get; set; } 

    [Column(TypeName = "bit")] 
    public bool Dormant { get; set; } 

    [StringLength(350)] 
    [Column(TypeName = "nvarchar")] 
    public string Description { get; set; } 

    public virtual ICollection<Contract> Contracts { get; set; } 

    public virtual ICollection<Owner> Owners { get; set; } 
} 

的观点是:

@model IEnumerable<IAR.ViewModels.OwnerListViewModel> 



<table class="table"> 

@foreach (var group in Model 
      .OrderBy(x => x.Dormant) 
      .GroupBy(x => x.Dormant)) 
{ 
    <tr class="group-header"> 
     @if (group.Key == true) 
     { 
      <th colspan="12"><h3>Dormant:- @group.Count()</h3></th> 
     } 
     else 
     { 
      <th colspan="12"><h3>Active:- @group.Count()</h3></th> 
     } 

    </tr> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.PostName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Created) 
     </th> 
     <th></th> 
    </tr> 

    foreach (var item in group 
       ) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.PostName) @Html.HiddenFor(modelItem => item.OwnerId) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Created) 
      </td> 
      <td> 
       @if (group.Key == true) 
       { 
        @Html.ActionLink("Make Active", "Dormant", new { ownerId = item.OwnerId }) 
       } 
       else 
       { 
        @Html.ActionLink("Make Dormant", "Dormant", new { ownerId = item.OwnerId }) 
       } 
      </td> 
     </tr> 
    } 
} 

</table> 

我敢肯定,这是简单的东西,但什么我不能做正确,以允许它从所有者引用Post?

+0

用HTTP装饰方法Post装饰 –

+0

究竟如何填充'db.Owners'?您的引用(“允许它引用帖子”)是正确的,但帖子不是*填充*。 –

+0

忘掉视图 - 它在到达视图之前会翻倒。 –

回答

2

鉴于s.Postnull,您将禁用实体框架的延迟加载。无论是启用延迟加载,或明确加载导航属性:

from s in db.Owners.Include(o => o.Post) 
orderby s.Post.PostName 
... 

至于你的编辑,给Owner.Post是在不同的数据库,这是一个完全不同的问题。在网上搜索,例如Cross database querying in EFJoining tables from two databases using entity framework以获得一些提示。可以在数据库级别链接它们(使用链接服务器或同义词和视图),或者在代码中修复它们(遍历Owners并查找它们的Post)。

+0

这在intellisense中有以下错误无法将lambda表达式转换为'string'类型,因为它不是委托类型' –

+1

而且如果您研究了该错误,则会发现需要'使用System.Data.Entity;'指令在您的文件顶部导入该扩展方法。 – CodeCaster

+0

谢谢,但是PostName在列表视图中为空 –

相关问题