0

I have set up 3 models, code first and the relationships seem to be working but one is causing me a problem.

I have Article, Language and Edition Classes

public class Article
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Icon { get; set; }
}

public class Language
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
}

public class Edition
{
    public int ID { get; set; }
    public Article Article  { get; set; }
    public Language Language { get; set; }

    public string Title { get; set; }        
    public string Details { get; set; }
}

In my bootstrap/DBinitialiser, I can create Objects and populate them fine. The DB is created and the foreign keys for Language and Article are both present on the Edition table and correctly entered.

var engLang = new Language() {Code="en", Name="English Language"};
var altLang = new Language() {Code="xx", Name="Alternative Language"};
db.Languages.Add(engLang);
db.Languages.Add(altLang);
db.SaveChanges();

var testArt = new Article() { Name = "test" };
db.Articles.Add(testArt);
db.SaveChanges();

db.Editions.Add(new Edition(){Article = testArt, Language = engLang, Title="English Content"});
db.Editions.Add(new Edition(){Article = testArt, Language = altLang, Title="Alternative Content"});
db.SaveChanges();

I can now query the Editions and return a list of them, but the Language attribute is always NULL. The Article Attribute works fine.

 var query = db.Editions.Where(r => r.Article.ID == Article.ID);

 foreach (Edition item in query)
 {
    // item.Language => NULL
    // item.Article => {Object Article}         
 }

I'm new to .net and Entity-Framework and can't work out why I always get this error. I can even query by r => r.Language.ID == 1 and still get a NULL attribute on the Edition object.

huwman
  • 86
  • 7

2 Answers2

1

Make sure you are using EF codefirst in right manner. Here you have some ambiguities. You must determine what relationships actually should exist, in your POCOs. Change classes like bellow:

public class Article
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Icon { get; set; }
}

public class Language
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
}

public class Edition
{
    [Key]
    public int ID { get; set; }
    public virtual Article Article  { get; set; }
    public virtual Language Language { get; set; }

    public string Title { get; set; }        
    public string Details { get; set; }
}
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
  • Thank you for your reply. That has helped me write my models better and the result is now no longer NULL. However the result is now: `'item.Language' threw an exception of type 'System.Data.EntityCommandExecutionException'` in addition, I have the message `There is already an open DataReader associated with this Command which must be closed first.` Which I believe is referring to lazy loading in the for loop? – huwman Jan 04 '13 at 09:01
  • 1
    To solve multiple DataReader add this to your connection string : MultipleActiveResultSets=true; This is Recommended by default in EF. – Amirhossein Mehrvarzi Jan 04 '13 at 15:19
1

With thanks to AmirHossein Mehrvarzi for helping me write my models more clearly, I believe this error to be caused by the lazy loading of entities while iterating through the result of the query. ref: Entity Framework: There is already an open DataReader associated with this Command.

Without enabling MultipleActiveResultSets I simply added an Include statement to my linq

var query = db.Editions.Where(r => r.Article.ID == Article.ID).Include(r => r.Language);

foreach (Edition item in query)
{
    // item.Language => {Object Language}
    // item.Article => {Object Article}         
}
Community
  • 1
  • 1
huwman
  • 86
  • 7