无法获得EF7(Entity Framework 7 Beta-7)来返回数据

时间:2015-10-04 14:54:45

标签: entity-framework asp.net-mvc-5 entity-framework-core

我有EF7设置和编译没有错误,我可以告诉它将SELECT命令发送到服务器,但返回的数据始终为null。

我没有使用DI,只在代码中设置EF。

上下文:

public class ApContext : DbContext
{
    public DbSet<FirmHdr> FirmHdr { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=azuresql.database.windows.net;Database=psAP;uid=xxxx;pwd=xxxx;");
    }
}

型号:

public class FirmHdr
{
    [Key]
    public Guid GID { get; set; }
    public DateTime DateModified { get; set; }
    public string FirmCode { get; set; }
    public string FirmName { get; set; }
    public DateTime DateStart { get; set; }
    public decimal BaseAmt { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Contact { get; set; }
    public string eMail { get; set; }
    public string FirmAdmin { get; set; }
    public string AdminPassword { get; set; }
}

获取方法:

public class ioEF7
{
    public List<FirmHdr> GetFirms()
    {
        using (var db = new ApContext())
        {
            var firms = from fx in db.FirmHdr orderby fx.FirmName select fx; //db.FirmHdr.ToList();
            var lfirms = firms.ToList();
            return lfirms;
        }

    }
}

无论我尝试什么,公司始终为空。任何帮助将不胜感激!

从SQL Server跟踪:

    SELECT [fx].[GID], [fx].[Address1], [fx].[Address2], [fx].[AdminPassword], [fx].[BaseAmt], [fx].[City], [fx].[Contact], [fx].[DateModified], [fx].[DateStart], [fx].[FirmAdmin], [fx].[FirmCode], [fx].[FirmName], [fx].[Phone], [fx].[State], [fx].[Zip], [fx].[eMail]
    FROM [FirmHdr] AS [fx]
    ORDER BY [fx].[FirmName]

SQL Manager中返回的数据:

    7e699f55-167e-41d8-b4d8-97d0ebd44da9    NULL    NULL    SOBHOST NULL    NULL    THIS IS A SPECIAL ACCOUNT ONLY FOR HOST ADMINS  2014-11-02 13:25:46 2014-11-02  SOB 0HOST   HOST SETUP ACCOUNT  NULL    NULL    NULL    NULL
    a96af7a2-5cf2-47f2-863c-7dc43bdd6a6a    NULL    NULL    NULL    100.0000    NULL    NULL    2014-11-20 21:40:51 2014-11-20  NULL    JOHNS   JOHNS FIRM  PHONE   NULL    NULL    NULL
    0094823d-ec85-4259-a094-3162673dcfd9    123 MAIN STREET STE 100       ADMINPASS 100.0000    MOBILE  JOHN SMITH  2014-11-01 17:50:57 2014-10-08  ADMIN   A456    SMITH & SMITH   251-345-6789    AL  36608   jsmith@smith.com
    f607a45d-71bb-4f73-ac49-858471ac67ae    NULL    NULL    PASS    NULL    NULL    NULL    2014-11-10 15:36:30 2014-11-10  ADMIN   TESTFIRM    TEST FIRM LLC   NULL    NULL    NULL    NULL
    9d8ec948-35e3-4daf-8523-feeec1252993    ADDRESS 1   ADDRESS 2   SOBPASS 83.3333 CITY    CONTACT 2014-11-01 14:21:26 2014-10-30  SOB A123    TEST FIRM NAME  123-456-7890    STATE   ZIP CODE    email@mydomain.com

因此,我可以使用EF发送到服务器但不在我的应用程序内部的确切SQL语句来获取数据。

2 个答案:

答案 0 :(得分:1)

检查您的表列类型和POCO类是否匹配。例如,如果 BaseAmt 的列可以为null,则匹配的CLR类型应为decimal?而不是decimal

答案 1 :(得分:0)

你是对的钱!我降级到EF6并在十进制字段上得到一个空值错误,所以我更正了模型,现在一切正常。从未在EF7中收到过该消息。

以下是更正的模型:

        public class FirmHdr
{
    [Key]
    public Guid GID { get; set; }
    public DateTime? DateModified { get; set; }
    public string FirmCode { get; set; }
    public string FirmName { get; set; }
    public DateTime? DateStart { get; set; }
    public decimal? BaseAmt { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Contact { get; set; }
    public string eMail { get; set; }
    public string FirmAdmin { get; set; }
    public string AdminPassword { get; set; }
}