Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Invalid Column Name
Message
De
24/06/2014 12:23:16
 
 
À
24/06/2014 11:50:53
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Versions des environnements
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01602256
Message ID:
01602305
Vues:
53
>>Hi,
>>
>>continuing work on my Web API project I have got 6 or 7 controllers to work but am getting an error on one of them that I cannot figure out:
>>
>>
innerException: {
>>message: "An error has occurred.",
>>exceptionMessage: "Invalid column name 'Route_rte_pk'. Invalid column name 'Route_rte_pk'.",
>
>>
>>The error refers to "Route_rte_pk", but the controller that is causing the problem is RunSheetHeaders which should not have any column referring to "Route" or "rte_pk".
>>
>>I have searched through the entire solution and not found this string anywhere.  Any ideas where I should look?
>>
>><b>< update ></b>
>>
>>I have looked into SQL Server Profiler and found that the sql being generated by Entity Framework is this:
>>
>>SELECT 
>>    1 AS [C1], 
>>    [Extent1].[rnh_pk] AS [rnh_pk], 
>>    [Extent1].[rnh_date] AS [rnh_date], 
>>    [Extent1].[rnh_rtefk] AS [rnh_rtefk], 
>>    [Extent1].[Route_rte_pk] AS [Route_rte_pk]
>>    FROM [dbo].[RunSheetHeader] AS [Extent1]
>>
>>Route is a separate table which is joined to this table by the rnh_rtefk = Route.rte_pk
>>
>>So I guess something is wrong in my model?
>>
>>Here's my model definition code:
>>
>><pre>    [Table("RunSheetHeader")]
>>    public partial class RunSheetHeader
>>    {
>>        public RunSheetHeader()
>>        {
>>            RunSheetDetails = new HashSet<RunSheetDetail>();
>>        }
>>
>>        [Key]
>>        public Guid rnh_pk { get; set; }
>>
>>        [Column(TypeName = "date")]
>>        public DateTime rnh_date { get; set; }
>>
>>        public Guid rnh_rtefk { get; set; }
>>
>>        public virtual Route Route { get; set; }
>>
>>        public virtual ICollection<RunSheetDetail> RunSheetDetails { get; set; }
>>    }
>>
>>Can anyone see what I have done wrong? Do I need to post more code?
>
>I think its your Route navigation property. I'm assuming rnh_rtefk is the foreign key used to relate your RunSheetHeader to the route. Since EF can't find a corresponding foreign key by convention for the Route table (see Relationship Convention at http://msdn.microsoft.com/en-us/data/jj679962), and you haven't specified what the foreign key is, it generates it for you.
>
>You'll want to specify the foreign key for the property either via the foreign key data annotation (http://msdn.microsoft.com/en-us/data/jj591583#Relationships) or with the fluent api (http://msdn.microsoft.com/en-us/data/jj591620#ForeignKey)

Thanks again Rob! I added this in to my model:
        [ForeignKey("Route")]
        public Guid rnh_rtefk { get; set; }
and that fixes it.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform