none
Invalid Column in Master Detail Relationship RRS feed

  • Question

  • I have two classes DC and Route (see below). DC has a 0 to many relationship with Route. While Route must have a single DC. I am using Entity Framework Code First (4.1). I believe I followed convention however when I try to access Routes in DC I get an invalid column. EFCF is building the SQL using DC_DCId instead of DCId. I tried adding a ForeignKey attribute on Routes but using the ForeignKey name on Routes did not work. Nor did using the DCId which is in Routes table. Any ideas? Also any good resources on how to debug these kinds of errors? I thought I was doing a pretty straight forward master detail relationship and EFCF should have been able to correctly infer what I want.

    public class DC {
    public int DCId { Get; Set; }
    public string Name { Get; Set; }
    public virtual ICollection<Route> Routes { Get; Set; }
    }

    public class Route {
    public int RouteId { Get; Set; }
    public string Name { Get; Set; }
    public virtual DC DC { Get; Set; }
    }

    Table DC {
    DCId int
    Name NChar(100)
    }

    Table Route {
    RouteId int
    Name NChar(100)
    DCId int
    }

    Saturday, October 1, 2011 1:14 AM

Answers

  • Hi

    If i understand correctly, yuo need to change Route class configuration to

     public class Route
        {
            public int RouteId { get; set; }
            public string Name { get; set; }
            [ForeignKey("DC")]
            public int DCId { get; set; }
            public virtual DC DC { get; set; }
        }
    

     

     

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Saturday, October 1, 2011 3:31 AM
  • Hi Rabi,

    Welcome!

    @Kris is right!

    You should add the foreign key in Rote class, you can use Data Annotation as @Kris suggestion, you can also use Fluent API to mapping the foreign key, you can refer here: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

     public class Route
        {
            public int RouteId { get; set; }
            public string Name { get; set; }
            public int DCId { get; set; }
            public virtual DC DC { get; set; }
        }
    
    modelBuilder.Entity<Route>() 
        .HasRequired(p => p.DC) 
        .WithMany(c => c.Routes) 
        .HasForeignKey(p => p.DCId);
    
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 5:29 AM
    Moderator

All replies

  • Hi

    If i understand correctly, yuo need to change Route class configuration to

     public class Route
        {
            public int RouteId { get; set; }
            public string Name { get; set; }
            [ForeignKey("DC")]
            public int DCId { get; set; }
            public virtual DC DC { get; set; }
        }
    

     

     

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Saturday, October 1, 2011 3:31 AM
  • Hi Rabi,

    Welcome!

    @Kris is right!

    You should add the foreign key in Rote class, you can use Data Annotation as @Kris suggestion, you can also use Fluent API to mapping the foreign key, you can refer here: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

     public class Route
        {
            public int RouteId { get; set; }
            public string Name { get; set; }
            public int DCId { get; set; }
            public virtual DC DC { get; set; }
        }
    
    modelBuilder.Entity<Route>() 
        .HasRequired(p => p.DC) 
        .WithMany(c => c.Routes) 
        .HasForeignKey(p => p.DCId);
    
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 5:29 AM
    Moderator