【问题标题】:Navigation properties are showing null randomly导航属性随机显示 null
【发布时间】:2013-01-17 13:47:32
【问题描述】:

考虑以下作为我的数据库的表结构。 (数据库:地理)

Country >> CountryId, CountryName
City >> CityId, CityName, LanguageId
Language >> LanguageId, LanguageName

以下是我的 WCF 项目中的一个方法(项目:Geography.WCFPortal)

[OperationContract]
public CountrySummary GetCountrySummary(string countryName)
{
CountrySummary countrySummaryRows = new CountrySummary();        
var result = from city in repository.GetQuery<City>()
                     .Include("Language")
                     .Where(city => city.Country.CountryName == countryName)
                     select city;

        countrySummaryRows.Country = this.GetCountry(countryName);

        foreach (var city in result.OrderByDescending(m => m.CityName).ToList())
        {
            countrySummaryRows.CityCollection.Add(city);
        }
        return countrySummaryRows;
}

CountrySummary 类的定义如下:(项目:Geography.Contracts)

[Serializable]
[DataContract]
public class CountrySummary
{
public CountrySummary()
{
this.CityCollection = new List<City>();
}

[DataMember]
public List<City> CityCollection { get; set; }

[DataMember]
public Country Country { get; set; }

}

我的 MVC 应用程序正在调用 GetCountrySummary 方法。

我的 MVC 视图之一是显示国家列表。它们每个都有一个“视图”链接,它调用 WCF 方法 (GetCountrySummary) 并将其显示在另一个视图上。

问题是 MVC 在某些城市的“语言”导航属性中随机接收 NULL。比如说,如果我第一次在印度点击“查看”,它工作正常,如果我下次点击它,它会给出“对象引用为空”的错误,当我检查我的“CountrySummary”对象时,它有某些城市的语言为 NULL(但它们在数据库中)。

每当我在 WCF 测试客户端中运行它时,它总是会填充货币。但在 MVC 应用程序中调用时有时会失败。

知道为什么会发生这种情况吗?

【问题讨论】:

  • 你在哪里创建存储库的实例?它是否被缓存并被重用?如果是这样,这可能就是问题所在。
  • 1) 我正在 WCF 项目的 Start 方法中注册我的存储库(我使用 WebActivator 注入 Start)。 2) 我没有做任何事情来缓存.. 3) MVC 项目中的 WCF 服务实例正在“Global.asax”事件中创建。

标签: asp.net-mvc wcf entity-framework


【解决方案1】:

我的猜测是存储库实例正在被重用,因为它在 WCF 启动事件中被初始化。尝试在 GetCountrySummary 方法中创建一个新实例以确认。

更多信息请参见this post

【讨论】:

    猜你喜欢
    • 2011-03-25
    • 2018-11-07
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多