结合MVC中2个数据库的数据

时间:2015-11-04 08:57:55

标签: c# asp.net asp.net-mvc entity-framework

我是asp.net mvc的新手,我目前有一个显示许多错误的应用程序。我有2个页面包含应用程序错误和日志错误。数据来自2个不同的数据库,但我想在一个页面上显示两个数据库中的数据。

表中有不同名称的标题,表示同样的事情,例如: Application Database中的ApplicationName与Log Database中的LogName相同。

以下是我目前所拥有的一个小例子,以及我想要的一个例子。

当前

Application Errors

ID   ApplicationName    ApplicationMessage     ApplicationDate

1    Something          Hello World            01/01/2015
2    Something Else     Another Message        03/01/2015


Log Errors

ID   LogName            LogMessage             LogDate

1    Some Log           A log message          02/01/2015
2    Another Log        Another Log Message    04/01/2015

我想要什么

Internal Errors

ID   Name               Message                Date

1    Something          Hello World            01/01/2015
2    Some Log           A log message          02/01/2015
3    Something Else     Another Message        03/01/2015
4    Another Log        Another Log Message    04/01/2015

在那一刻,我为每个数据库设置了2个独立的模型,但我认为我需要将两个模型合并为一个模型,将两者结合起来,但我不确定如何做到这一点。如何将两个数据源合并在一起以在同一页面中显示数据?

当前模型

应用

    [Table("ELMAH_Error")]
    public class ElmahError
    {
        [Key]
        public System.Guid ErrorId { get; set; }

        public System.String Application { get; set; }
        public System.String Host { get; set; }
        public System.String Type { get; set; }
        public System.String Source { get; set; }
        public System.String Message { get; set; }
        public System.String User { get; set; }
        public System.Int32 StatusCode { get; set; }
        public System.DateTime TimeUtc { get; set; }
        public System.Int32 Sequence { get; set; }
        public System.String AllXml { get; set; }
    }

日志

    [Table("LogEntry")]
    public class LogEntry
    {
        [Key]
        public Int64 ID { get; set; }
        public DateTime LogDate { get; set; }
        public Int16 Priority { get; set; }
        public string SourceClass { get; set; }
        public string Category { get; set; }
        public string Message { get; set; }
        public string UserID { get; set; }
        public string ProcessID { get; set; }
    }

从模型中,我想要合并许多字段以及与我想要包含的字段不相似的字段。下面的模型显示了我想要的内容,但我不知道如何实现它。

内部错误

    public class InternalErrors
    {

        public string Id { get; set; } //L:ID && E:ErrorId
        public int Priority { get; set; } //L:Priority
        public string Application { get; set; } //L:SourceClass && E:Application
        public string Message { get; set; } //L:Message && E:Message
        public string Type { get; set; } //L:Category && E:Type
        public string User { get; set; } //L:UserID && E:User
        public string ProcessID { get; set; } //L:ProcessID
        public DateTime Date { get; set; } //L:LogDate && E:TimeUtc
        public int StatusCode { get; set; } //E:StatusCode
        public string AllXml { get; set; } //E:AllXml
        public int Sequence { get; set; } //E:Sequence
        public int ErrorCount { get; set; } //E:ErrorCount
    }

我希望这些信息足以让您提供答案,如果您还需要其他信息,请告诉我。

提前致谢

4 个答案:

答案 0 :(得分:2)

如果你想要的是这个

Internal Errors

ID   Name               Message                Date

1    Something          Hello World            01/01/2015
2    Some Log           A log message          02/01/2015
3    Something Else     Another Message        03/01/2015
4    Another Log        Another Log Message    04/01/2015

然后创建一个名为InternalErrors的类,如下所示。

public class InternalErrors
{
       public int ID;
       public string Name;
       public string Message;
       public DateTime Date;
}

现在,您可以按如下方式编写Linq查询,以从应用程序错误和日志错误中获取数据,并在其上执行联合。

var AppErrors=from AE in _db.ApplicationErrors select AE;
var LogErrors=from LE in _dc.LogErrors select LE;
var internerrors=AppErrors.Union(LogErrors);
var InternalErrors=(from ie in internerrors select new InternalErrors()
                      {
                          ID=ie.ID,
                          Message=ie.ApplicationMessage,
                          Name=ie.ApplicationName,
                          Date=ie.ApplicationDate
                      }).ToList();

答案 1 :(得分:2)

来自MRebati的viewmodel方法是最佳解决方案。

我经常发现有一个基类和不同的实现是有用的:

public abstract class ErrorViewModel
{

    public abstract int Id { get; }

    public abstract string Name { get; }
}

public class ElmahErrorViewModel
{
    public ElmahErrorViewModel(ElmahError instance)
    {
        this.Instance = instance;
    }

    public ElmahError Instance { get; private set; }

    public int Id { get { return Instance.ErrorId; } }

    public string Name { get { return instance.Appication; } }
}

通过这种方式,您可以创建List<ErrorViewModel>并使用

添加条目
var items = from e in context.ElmahErrors
            select new ElmahErrorViewModel(e);

list.AddRange(items);

var items2 = from l in context.LogEntrys
             select new LogEntryViewModel(l);

list.AddRange(items2);

这非常有用,因为您隐藏了详细信息,但仍然可以分隔列表并使用

访问基础对象
var elmahErrors = items.OfType<ElmahErrorViewModel>().Select(x => x.Instance);

答案 2 :(得分:1)

有许多方法可以将模型中的数据提供给View。 一个是ViewModel。它必须包含要发送到视图的数据。看看这个:

using System;

public class ErrorViewModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Message { get; set; }

    public DateTime Date { get; set; }
}

在Controller中,您需要创建此ViewModel的列表并使用您的数据填充它。

答案 3 :(得分:1)

您可以使用using System; using System.Linq; using System.Collections.Generic; public class Program { public static void Main() { var elmahErrorList = new List<ElmahError>{ new ElmahError{ ErrorId = Guid.NewGuid(), Application = "Something",Message = "Hello World" , TimeUtc = DateTime.Now }, new ElmahError{ ErrorId = Guid.NewGuid(), Application = "Something Else",Message = "Another Message" , TimeUtc = DateTime.Now } }; var logEntryList = new List<LogEntry>{ new LogEntry{ ID = 1, SourceClass = "Something",Message = "Hello World" , LogDate = DateTime.Now }, new LogEntry{ ID = 1, SourceClass = "Something Else",Message = "Another Message" , LogDate = DateTime.Now } }; var internalErrorsList = new List<InternalErrors>(); var elmahErrorListinternalErrorses = elmahErrorList.Select(e => new InternalErrors { Id = e.ErrorId.ToString(), Application = e.Application, Message = e.Message, Type = e.Type, User = e.User, Date = e.TimeUtc, StatusCode = e.StatusCode, AllXml = e.AllXml, Sequence = e.Sequence }); internalErrorsList.AddRange(elmahErrorListinternalErrorses); var elmahErrorListlogEntryLists = logEntryList.Select(l => new InternalErrors { Id = l.ID.ToString(), Priority = l.Priority, Application = l.SourceClass, Message = l.Message, Type = l.Category, User = l.UserID, Date = l.LogDate }); internalErrorsList.AddRange(elmahErrorListlogEntryLists); internalErrorsList.ForEach(f => { Console.Write(f.Id); Console.Write("\t"); Console.Write(f.Application);Console.Write("\t"); Console.Write(f.Message);Console.Write("\t"); Console.Write(f.Date);Console.Write("\t"); Console.WriteLine(); }); } public class InternalErrors { public string Id { get; set; } //L:ID && E:ErrorId public int Priority { get; set; } //L:Priority public string Application { get; set; } //L:SourceClass && E:Application public string Message { get; set; } //L:Message && E:Message public string Type { get; set; } //L:Category && E:Type public string User { get; set; } //L:UserID && E:User public string ProcessID { get; set; } //L:ProcessID public DateTime Date { get; set; } //L:LogDate && E:TimeUtc public int StatusCode { get; set; } //E:StatusCode public string AllXml { get; set; } //E:AllXml public int Sequence { get; set; } //E:Sequence public int ErrorCount { get; set; } //E:ErrorCount } public class ElmahError { public System.Guid ErrorId { get; set; } public System.String Application { get; set; } public System.String Host { get; set; } public System.String Type { get; set; } public System.String Source { get; set; } public System.String Message { get; set; } public System.String User { get; set; } public System.Int32 StatusCode { get; set; } public System.DateTime TimeUtc { get; set; } public System.Int32 Sequence { get; set; } public System.String AllXml { get; set; } } public class LogEntry { public Int64 ID { get; set; } public DateTime LogDate { get; set; } public Int16 Priority { get; set; } public string SourceClass { get; set; } public string Category { get; set; } public string Message { get; set; } public string UserID { get; set; } public string ProcessID { get; set; } } }

{{1}}

演示:https://dotnetfiddle.net/mrWGDn