【问题标题】:Reference one model from another model in another database asp.net mvc从另一个数据库中的另一个模型引用一个模型 asp.net mvc
【发布时间】:2015-09-28 10:51:04
【问题描述】:

我的控制器中有以下方法:

    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>

我确定这很简单,但我做错了什么以允许它引用所有者的帖子?

【问题讨论】:

  • 用 HTTP Post 装饰来装饰方法
  • 究竟如何填充 db.Owners ?您的引用(“允许它引用所有者的帖子”)是正确的,但帖子未填充
  • 忘记视图 - 它在到达视图之前就倒下了。

标签: c# asp.net-mvc


【解决方案1】:

鉴于 s.Postnull,您已禁用 Entity Framework 的延迟加载。要么启用延迟加载,要么显式加载导航属性:

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 并查找它们的 Posts)。

【讨论】:

  • 这在智能感知中有以下错误无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型'
  • 如果您研究了该错误,您会发现您需要在文件顶部使用 using System.Data.Entity; 指令来导入该扩展方法。
  • 谢谢,但是列表视图中的 PostName 是空白的
  • 这是一个不同的问题。您确定此所有者设置了 PostID?
  • 查看所有者表后,保存方法添加了一个自动生成的 PostId 编号,而不是选择的实际 PostId,尽管我无法弄清楚原因
猜你喜欢
  • 2011-05-13
  • 2012-12-13
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 2016-05-09
相关资源
最近更新 更多