0

I have a simple one to many relationship between my PolicyVIEInfo class TriggerInfo class. I would like to include the PolicyVIEInfo_Id attribute in the TriggerInfo class but when I add the property I get

Invalid column name 'PolicyVIEInfo_Id1'. I'm assuming it is adding the one because it already knows about the foreign key, but how do I add it to my class and map it properly ??

[DataContract]
public class PolicyVIEInfo : IdentifiableEntity
{
    [DataMember]
    public HashSet<TriggerInfo> TriggerInfos  { get; set; }
}
[DataContract]
public class TriggerInfo : IdentifiableEntity
{
    [DataMember]
    [Required]
    public long PolicyVIEInfo_Id { get; set; }
    [DataMember]
    public bool? TriggerBreached { get; set; }
}

[DataContract]
public abstract class IdentifiableEntity
{

    [DataMember]
    [Key]
    public long Id { get; set; }
}

I don't want to include the entire PolicyVIEInfo because of circular reference problems. Any help would be greatly appreciated this has been driving me nuts for days. Using fluent api hasn't helped. From what I've read I am using regular conventions so I thought it would just work.

Mitul
  • 9,734
  • 4
  • 43
  • 60
quigley
  • 21
  • 2

1 Answers1

0

I believe there are three options which all should work equally well:

  • Either remove the underscore because it is against the mapping convention rules:

    public long PolicyVIEInfoId { get; set; }
    
  • Or add a data annotation attribute:

    [ForeignKey("PolicyVIEInfo_Id")]
    public HashSet<TriggerInfo> TriggerInfos  { get; set; }
    
  • Or use Fluent API:

    modelBuilder.Entity<PolicyVIEInfo>()
        .HasMany(p => p.TriggerInfos)
        .WithRequired()
        .HasForeignKey(t => t.PolicyVIEInfo_Id);
    

If you map to an existing database, that is the FK column with name PolicyVIEInfo_Id already exists in the database, for the first option you have to add a mapping from the PolicyVIEInfoId property name to the PolicyVIEInfo_Id column name, either by using the [Column("PolicyVIEInfo_Id")] attribute or by adding .Property(t => t.PolicyVIEInfoId).HasColumnName("PolicyVIEInfo_Id") with Fluent API.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • I thought mapping convention rules were any combination of the class name plus "ID". The relationship is already working by convention. If I add a "PolicyVIEInfo PolicyVIEInfo" attribute to my TriggerInfo class it is populated automatically. But I just want the I'd not the whole class. – quigley Sep 19 '13 at 10:39