C#OData DBModelBuilder值为空

时间:2018-11-14 08:20:47

标签: c# odata

我是C#OData编码的新手,请尝试帮助我。

我有两个模型User Model和MobileName Model ...我想从MobileName模型获取User的MobileName,输出应该像我给定的JSON值所示

用户模型

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

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string EmailAddress { get; set; }

    public virtual MobileName MobileName { get; set; }
}

手机名称模型

public class MobileName 
{
    public int Id { get; set; }
    public User User { get; set; }
    public string Name { get; set; }
}

预期的JSON输出

 "User": {
          "Id": 1234,
          "FirstName": "Hello",
          "LastName": "World",
          "EmailAddress": "hello@world.com"
          "MobileName": "HelloMobile"
  }

在C#代码中,我这样叫DBModelBuilder

var user = mBuilder.Entity<Models.User>().ToTable("User");
user.HasOptional(x=>x.MobileName).WithRequired(x => x.User);

MobileName值将为null ...但是当我在数据库中使用此SQL查询时,我会完美地获取该值

查询

Select Id, Name from MobileName where UserId=1234

0 个答案:

没有答案